In C + +, you can specify a default value for the parameter. Default parameters are automatically used when a function call does not specify an argument that corresponds to a formal parameter.
Syntax and use of default parameters:
(1) Assign a value directly to a parameter when the function is declared or defined. This is the default parameter;
(2) Omit some or all of the arguments when the function is called. You can use the default argument instead.
Attention:
(1) The default parameter can only be set once in the function declaration. You can set it in the function definition only when there is no function declaration. (#add, this sentence means the existence of two parts of function declaration and definition. Validation indicates that this limitation can be casual, but specified in the Declaration for specification
(2) If a parameter has a default value, the parameters on the right side must have a default value. (#add when this is defined, the parameter table of the member function of the class is declared at the right of the parameter table, and how it is to be summarized when used)
For example: int mal (int a, int b=3, int c=6, int d=8) correct, set default value in Right-to-left order.
int mal (int a=6, int b=3, int c=5, int d) error, not setting default value from right to left. C Sets the default value, and the D on the right does not have a default value.
(3) When the default parameter is called, it follows the order of the parameter invocation, called from left to right. This point should be clearly divided with paragraph (2) and should not be confused. What's the meaning of the horse #add? Tentatively understood that the arguments between the two default parameters must be assigned a default value, which means that the first argument from left to right is the argument of the first parameter, and so on, when the function is invoked.
such as: void Mal (int a, int b=3, int c=5); Default parameters
Mal (3, 8, 9); When called with the specified arguments, the default argument is not used
Mal (3, 5); When called, only two parameters are specified, called from left to right, equivalent to Mal (3,5,5);
Mal (3); When called, only 1 parameters are specified, called from left to right, equivalent to Mal (5,3,5);
Mal (); Error because a has no default value
Mal (3,, 9)//error, should be called from left to right
Further such as: void Mal (int a=8, int b=3, int c=5); Default parameters
Mal (); Correct, call all default parameters, equivalent to Mal (8,3,5);
(4) The default value can be a global variable, a global constant, or even a function. But it cannot be a local variable. Because the invocation of the default parameter is determined at compile time, the local variable location and default value cannot be determined at compile time.
Original quote: http://blog.csdn.net/qingyue_bao/article/details/6582242