1. Default parameters
The default parameters are "spare tires" in terms of image.
1.1 Default Parameter Concept
The default parameter is to specify a default value for the function's parameters when declaring or defining a function. When calling this function, the default value is used if no actual parameter is specified, otherwise the specified actual parameter is used.
#include<iostream> void P(int a = 3) { printf("%d", a); } int main() { P();//Print default value 3 P(1);//Print the transmitted value 1 return 0; }
1.2 Default parameter classification
1. All default parameters
void P(int a = 3,int c = 1,int b = 2) { printf("%d", a); printf("%d", b); printf("%d", c); }
2. Function overloading
2.1 Function overload concept
Function overloading: It is a special case of functions. C++ allows several functions with the same name to be declared in the same scope. The formal parameter lists (number of parameters or type or order) of these functions with the same name must be different. They are often used to deal with the problem of implementing different data types of functions similar to those of the same name.
int Add(int left, int right) { return left+right; } double Add(double left, double right) { return left+right; } long Add(long left, long right) { return left+right; } int main() { Add(10, 20); Add(10.0, 20.0); Add(10L, 20L); return 0; }
Observe the functions above and we will find that their function names are the same and their parameters are different (parameter type, parameter order, etc.) Only when the above conditions are met can function reload
Notice: Functions of different return types cannot be overloaded!
double Add(double left, double right) { return left+right; } long Add(double left, double right) { return left+right; }
This is the wrong function overload
2.2 Name modification
Here we think about a question, why does C++ support function overloading but C language does not?
In C/C++, a program needs to go through the following stages to run: preprocessing, compilation, assembly, and linking
1. In fact, our project is usually composed of multiple header files and multiple source files. For example, when (the function add is called in the (in the link), the file does not have the add function address in the file before the link.
2. So the linking stage is to solve this problem. When the linker sees that add is called but there is no address of add, he will go to the symbol table to find the address of add and then link it together.
3. When linking, the function name modification rules of each compiler are different, which may lead to different links. For example, the name remains unchanged after the function modification under the gcc compiler. After the g++ function is modified, it becomes [_Z+function length + function name + type initial letter].
++ supports function overloading but C does not support function overloading because: C language cannot distinguish between function names when the same as function names, while C++ can distinguish functions with the same name by the different function parameters!
This is the end of this article about C++ briefly explaining the usage of default parameters and function overloading. For more related contents of C++ default parameters and function overloading, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!