1. Function
1.1 Overview
Function: Encapsulate a piece of frequently used code to reduce repeated code. A larger program is generally divided into several program blocks, and each module implements a specific function.
1.2 Definition of function
The definition of a function generally has 5 steps:
1. Return value type
2. Function name
3. Parameter list
4. Function body statement
5. Return expression
grammar:
返回值类型 函数名 (参数列表)
{
函数体语句
return 表达式
}
- Return type: A function can return a value. in the function definition
- Function name: give the function a name
- Parameter list: When using this function, the data passed in
- Function body statement: the code inside curly braces, the statement that needs to be executed in the function
- return expression: linked to the return value type, after the function is executed, the corresponding data is returned
Example: Define an addition function to add two numbers
//函数定义
int add(int num1, int num2)
{
int sum = num1 + num2;
return sum;
}
1.3 Function call
Function: use the defined function
grammar: 函数名(参数)
Example:
#include<iostream>
using namespace std;
//函数定义
int add(int num1, int num2) //定义中的num1,num2称为形式参数,简称形参
{
int sum = num1 + num2;
return sum;
}
int main() {
int a = 10;
int b = 10;
//调用add函数
int sum = add(a, b);//调用时的a,b称为实际参数,简称实参
cout << "sum = " << sum << endl;
a = 100;
b = 100;
sum = add(a, b);
cout << "sum = " << sum << endl;
return 0;
}
Summary: The parentheses in the function definition are called formal parameters, and the parameters passed in when the function is called are called actual parameters
1.4 Passing by value
- The so-called passing by value means that when the function is called, the actual parameter passes the value to the formal parameter.
- When the value is passed,If the formal parameter occurs, it does not affect the actual parameter
Example:
#include<iostream>
using namespace std;
void swap(int num1, int num2)
{
cout << "交换前:" << endl;
cout << "num1 = " << num1 << endl;
cout << "num2 = " << num2 << endl;
int temp = num1;
num1 = num2;
num2 = temp;
cout << "交换后:" << endl;
cout << "num1 = " << num1 << endl;
cout << "num2 = " << num2 << endl;
//return ; 当函数声明时候,不需要返回值,可以不写return
}
int main() {
int a = 10;
int b = 20;
swap(a, b);
cout << "mian中的 a = " << a << endl;
cout << "mian中的 b = " << b << endl;
return 0;
}
Summary: When passing by value, formal parameters cannot modify actual parameters
1.5 Common Styles for Functions
There are 4 common function styles:
- no return
- Participation but no return
- Return without participation
- Participate and return
Example:
#include<iostream>
using namespace std;
//函数常见样式
//1、 无参无返
void test01()
{
//void a = 10; //无类型不可以创建变量,原因无法分配内存
cout << "this is test01" << endl;
//test01(); 函数调用
}
//2、 有参无返
void test02(int a)
{
cout << "this is test02" << endl;
cout << "a = " << a << endl;
}
//3、无参有返
int test03()
{
cout << "this is test03 " << endl;
return 10;
}
//4、有参有返
int test04(int a, int b)
{
cout << "this is test04 " << endl;
int sum = a + b;
return sum;
}
1.6 Declaration of functions
Function: Tell the compiler the name of the function and how to call the function. The actual body of the function can be defined separately.A function can be declared multiple times , but a function can only be defined once
Example:
#include<iostream>
using namespace std;
//声明可以多次,定义只能一次
//声明
int max(int a, int b);
int max(int a, int b);
//定义
int max(int a, int b)
{
return a > b ? a : b;
}
int main() {
int a = 100;
int b = 200;
cout << max(a, b) << endl;
return 0;
}
1.7 Writing functions in separate files
Function: Make the code structure clearer
There are generally 4 steps to write function files
- Create a header file with the suffix .h
- Create a source file with a suffix of .cpp
- Write function declarations in header files
- Write the definition of the function in the source file
Example:
//swap.h文件
#include<iostream>
using namespace std;
//实现两个数字交换的函数声明
void swap(int a, int b);
//swap.cpp文件
#include "swap.h"
void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
//main函数文件
#include "swap.h"
int main() {
int a = 100;
int b = 200;
swap(a, b);
return 0;
}
2. Function improvement
2.1 Function default parameters
In C++, the formal parameters in the formal parameter list of the function can have default values.
grammar: 返回值类型 函数名 (参数= 默认值){}
Example:
#include<iostream>
using namespace std;
int func(int a, int b = 10, int c = 10) {
return a + b + c;
}
//1. 如果某个位置参数有默认值,那么从这个位置往后,从左向右,必须都要有默认值
//2. 如果函数声明有默认值,函数实现的时候就不能有默认参数
int func2(int a = 10, int b = 10);
int func2(int a, int b) {
return a + b;
}
int main() {
cout << "ret = " << func(20, 20) << endl;
cout << "ret = " << func(100) << endl;
return 0;
}
2.2 Function placeholder parameters
There can be placeholder parameters in the formal parameter list of a function in C++, which are used as placeholders, and the place must be filled when the function is called.
grammar: 返回值类型 函数名 (数据类型){}
Example:
#include<iostream>
using namespace std;
//函数占位参数 ,占位参数也可以有默认参数
void func(int a, int) {
cout << "this is func" << endl;
}
int main() {
func(10,10); //占位参数必须填补
return 0;
}
2.3 Function overloading
Function: the function name can be the same, improving reusability
Function overloading satisfies the conditions:
- under the same scope
- same function name
- The function parameters are of different types or numbers or orders
Note: The return value of a function cannot be used as a condition for function overloading
Example:
#include<iostream>
using namespace std;
//函数重载需要函数都在同一个作用域下
void func()
{
cout << "func 的调用!" << endl;
}
void func(int a)
{
cout << "func (int a) 的调用!" << endl;
}
void func(double a)
{
cout << "func (double a)的调用!" << endl;
}
void func(int a ,double b)
{
cout << "func (int a ,double b) 的调用!" << endl;
}
void func(double a ,int b)
{
cout << "func (double a ,int b)的调用!" << endl;
}
//函数返回值不可以作为函数重载条件
//int func(double a, int b)
//{
// cout << "func (double a ,int b)的调用!" << endl;
//}
int main() {
func();
func(10);
func(3.14);
func(10,3.14);
func(3.14 , 10);
return 0;
}
Notice:
- references as overload conditions
- Function overloading encounters function default parameters
Example:
#include<iostream>
using namespace std;
//函数重载注意事项
//1、引用作为重载条件
void func(int &a)
{
cout << "func (int &a) 调用 " << endl;
}
void func(const int &a)
{
cout << "func (const int &a) 调用 " << endl;
}
//2、函数重载碰到函数默认参数
void func2(int a, int b = 10)
{
cout << "func2(int a, int b = 10) 调用" << endl;
}
void func2(int a)
{
cout << "func2(int a) 调用" << endl;
}
int main() {
int a = 10;
func(a); //调用无const
func(10);//调用有const
//func2(10); //碰到默认参数产生歧义,需要避免
return 0;
}