1. Default parameters
Classification of default parameters: full default, semi-default
The default parameter is to specify a default value for the parameter of the function when declaring or defining the function
. When calling the function,若没有指定实参则采用该形参的缺省值
否则使用指定的实参
void Func(int a = 0)
{
cout<<a<<endl;
}
int main()
{
Func(); // 没有传参时,使用参数的默认值
Func(10); // 传参时,使用指定的实参
return 0;
}
1.1 Classification of default parameters
-
All default parameters
void Func(int a = 10, int b = 20, int c = 30)
{
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
cout<<"c = "<<c<<endl;
}
Here, we should pay attention to the values that are given next to each other from left to right when we pass parameters.
Fun(15, 25); if the parameter is passed in this way, the default value of c is 30
Note: parameter passing cannot be written in this form
Func(,25,);
Func(,,35);
Func(,25,35);
Func(15,,35);
-
default parameters
void Func(int a, int b = 10, int c = 20)
{
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
cout<<"c = "<<c<<endl;
}
As long as any parameter is not given a default value, it is semi-default
Notice:
The semi-default parameters must
从右往左
be given in order, and cannot be given in intervals.Default parameters cannot appear in both function declaration and definition
The default value must be
常量
either全局变量
2. Function overloading
Function overloading is a special case of functions. C++ allows several functions of the same name with similar functions to be declared in the same scope. These functions with the same name have different formal parameter lists (number of parameters or type or type order), which are often used to deal with implementation . The function is similar to the problem of different data types.
2.1 different parameter types
// 1、参数类型不同
int Add(int left, int right)
{
cout << "int Add(int left, int right)" << endl;
return left + right;
}
double Add(double left, double right)
{
cout << "double Add(double left, double right)" << endl;
return left + right;
}
2.2 The number of parameters is different
void f()
{
cout << "f()" << endl;
}
void f(int a)
{
cout << "f(int a)" << endl;
}
2.3 The order of parameter types is different
// 3、参数类型顺序不同
void f(int a, char b)
{
cout << "f(int a,char b)" << endl;
}
void f(char b, int a)
{
cout << "f(char b, int a)" << endl;
}
Note: Different return values cannot constitute function overloading
3. Why does C program not support function overloading?
In C/C++, to run a program, it needs to go through the following stages: preprocessing, compiling, assembling, linking
- Preprocessing stage: main content: header file expansion, macro replacement conditional compilation, remove comments
- Compilation phase: main content: syntax checking and assembly code generation
- Assembly stage: main content: form a symbol table, convert assembly code into binary code so that the machine can understand it
- Linking process: merging segment tables; merging of symbol tables and redefinition of symbol tables
Linking process: the object files of .o will be merged together, and then you need to find some function addresses that only give the declared functions
而每一个.o文件都有一个符号表符号表中存放函数的地址
When the main file wants to call this function, it will go to the symbol table to find the address of the function
而符号表中两个func函数的地址
编译器不知道应该调用哪个
所以c程序不支持函数重载