A detailed explanation of the default value of C + + function parameters _c language

Source: Internet
Author: User
Tags function definition function prototype

In general, when the parameter is taken from the argument from the function call, the number of arguments should be the same as the formal parameter. Sometimes the same function is invoked multiple times with the same arguments, C + + provides a simple way to give formal parameters a default value, so that the formal parameter does not have to be a value. If there is a function declaration
float Area (float r=6.5);
Specifies that the default value of R is 6.5, and if you confirm that R's value is 6.5 when you call this function, you do not have to give the value of the argument, such as
Area (); Equivalent to area (6.5);
If you do not want the parameter to take this default value, it is given separately by the argument. such as area (7.5); The parameter gets a value of 7.5 instead of 6.5.
This method is more flexible and can simplify programming and improve 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 part of the parameter, and the other part does not specify a default value. If there is a function for the volume of a 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);Specify a default value of 12.5 for formal parameter R only
function calls can take the following form:
Volume (45.6); Equivalent to Volume (45.6,12.5)
Volume (34.2,10.4)//h has a value of 34.2,r of 10.4
The combination of the actual participating formal parameters is done from left to right. Therefore, the parameter that specifies the default value must be placed at the very right end of the formal parameter list column, otherwise an error occurs.For example:
void F1 (float a,int b=0,int C,char d=′a′); Not correct
void F2 (float a,int c,int b=0, char d=′a′); That's right
If you call the F2 function above, you can take the following form:
F2 (3.5, 5, 3,′x′)//parameter values all from the argument
F2 (3.5, 5, 3)//The value of the last formal parameter takes the default value ′a′
F2 (3.5, 5)//The last two parameter values are the default values, b=0,d=′a′
As you can see, when you invoke a function that has a default parameter, the number of arguments can be different from the number of formal parameters, and the argument is not given, which is worth the default value from the formal parameter. Using this feature, you can make the function more flexible. For example 4.7, find the maximum number of 2 or 3 digits. You can also use a function with a default parameter instead of overloading the function.
For Example 4.8, find the maximum number of 2 or 3 positive integers, implemented with a function with default parameters.

Copy Code code as follows:

#include <iostream>
using namespace Std;
int main ()
{int max (int A, int b, int c=0);/function declaration, parameter C has default value
int a,b,c;
cin>>a>>b>>c;
Cout<<″max (a,b,c) =″<<max (a,b,c) <<endl; Output the largest of the 3 numbers
Cout<<″max (a,b) =″<<max (a,b) <<endl; Output the largest of the 2 numbers
return 0;
}
int max (int a,int b,int c)//function definition
{if (b>a) a=b;
if (c>a) a=c;
return A;
}

The operating conditions are as follows:
14-56 135↙
Max (A,B,C) =135

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.