1. The default value of the parameter may be declared in the function prototype, and the default value is a value that is used when no parameter value is supplied.
such as function prototype long myFunction (int x=50);
The prototype indicates a parameter default value of 50, which is not affected by the declaration of parameter values in the prototype and function definitions, which can also be used:
Long myFunction (int x) {function definition};
If the function MyFunction () is called without a given argument, then the default value is used X=50
2, parameter Default value NOTE: If a parameter does not have a default value, then all of its previous parameters must not use the default value.
such as long myFunction (int param1,int param2,int param3), the PARAM3 default value can be given only when the default value is used for param2, and only if param3, Param2 is given a default value to give the param1 default value. (The default value is given in the right-to-left order, because the rule: the parameter sets the default value from right to left, and the argument is assigned to the parameter from left to right )
3. Use an instance of the default parameter (21 days to learn C + + P846.7): Write a function to calculate the cube volume.
#include <iostream>using namespacestd;//int areacube (int length,int width=25,int height=1);intCubevolume (intLengthintWidth= -,intheight=1)///Note that if set to
int cubevolume (int length=25,int width=1,int height)
You will get an error because the default parameter is set from right to left in the formal parameterintMain () {intLength= -, width= -, height=Ten; intvolume; Volume=Cubevolume (length,width,height); cout<<"firt cube equals:"<<volume; Volume = Cubevolume (length,width);//What default parameter you want to use, it is OK to call the function directly without calling this parameter cout<<"second cube equals:"<<volume; Volume=cubevolume (length); cout<<"third cube equals:"<<volume; System ("PAUSE"); return 0;}intCubevolume (intLengthintWidthintheight) { return(length*width*height);}
Default parameters in C + +