C++ function parameters take default values in depth

  • 2020-04-02 01:05:58
  • OfStack

Normally, when a function is called, the arguments get their value from the arguments, so the number of arguments should be the same as the arguments. Sometimes the same arguments are used for multiple calls to the same function. C++ provides an easy way to do this by giving the arguments a default value so that they don't have to be taken from the arguments. If there is a function declaration
Float area (float r = 6.5);
Specify that the default value of r is 6.5, and if the value of r is confirmed to be 6.5 when this function is called, you do not have to give the value of the argument, such as
Area (s);   // area(6.5);
If you do not want the parameter to take this default value, the argument is given separately. Such as area (7.5); // the parameter is 7.5 instead of 6.5
This method is more flexible, can simplify the programming, improve the operation efficiency.
If you have more than one parameter, you can have a default value for each parameter, or you can specify a default value for only one parameter and no default value for another parameter. If there is a function to find the volume of the cylinder, the parameter h represents the height of the cylinder, and r is the radius of the cylinder. The function prototype is as follows:
Float volume (float h, float r = 12.5).   // only the default value of parameter r is 12.5
Function calls can take the following form:
Volume (45.6);             // equivalent to volume(45.6,12.5)
Volume (34.2, 10.4)     //h is 34.2 and r is 10.4
The combination of real parameters is from left to right. Therefore, the parameter that specifies the default value must be placed at the rightmost end of the parameter table column, otherwise an error occurs. Such as:
Void f1(float a, int b=0, int c, char d= 'a');         / / is not correct
Void f2(float a, int c, int b=0, char d= 'a');         / / right
If you call the f2 function above, you can take the following form:
The value of f2(3.5, 5, 3, 'x') // parameter is obtained from the arguments
F2 (3.5, 5, 3)           // the default value of the last parameter is' a '
F2 (3.5, 5)                 // the default values of the last two parameters are: b=0,d= 'a'
As you can see, when a function with default arguments is called, the number of arguments can be different from the number of arguments, from the default value to the value of arguments that are not given. Taking advantage of this feature makes the use of functions more flexible. For example, example 4.7 find the largest number of two or three Numbers. You can also use a function with default parameters instead of an overloaded function.
Example 4.8 find the largest number of two or three positive integers, using a function with default parameters.


 #include <iostream>
using namespace std;
int main( )
{int max(int a, int b, int c=0);//Function declaration, parameter c has a default value
int a,b,c;
 cin>>a>>b>>c;
 cout<< " max(a,b,c)= " <<max(a,b,c)<<endl;   //The largest of the three outputs
 cout<< " max(a,b)= " <<max(a,b)<<endl;       //The largest of the two outputs
 return 0;
}
int max(int a,int b,int c)        //The function definitions
{if(b>a) a=b;
 if(c>a) a=c;
 return a;
}

The operation is as follows:
14   - 56   135 �
Max (a, b, c) = 135


Related articles: