1. Functions for default parameter values
The C + + language allows you to specify a default value for a function when it is defined or some form parameter (parameter) by writing "= Default" after the corresponding parameter, and if the value of the argument at the corresponding location is omitted, the default value of the parameter is evaluated when the function is executed.
I self-study, the textbook may be a bit old, there are problems to ask you correct!!! Thank you!!!
2. Default parameter value function considerations
- The default function is typically set in the function declaration. If there are function declarations and function definitions in the program, the default values of the parameters are not allowed to be defined, and if there are only function definitions in the program and no function declarations, the default parameters can appear in the function definition;
- The order of the default parameters. If there are multiple default parameters in a function, the default parameters in the parameter distribution should be defined individually from right to left. That is, if you want to give the default value of this parameter to the right side of the parameter, then the definition is wrong. Such as:
void int a=1floatlong c= ); // Error void int float b=2long c= ); // correct
Example: Define and test overloaded functions.
#include <iostream>using namespacestd;intSumintIintj=Ten);//there is a function declarationintSumintIintj) { returni+J;}voidPrintintIintJintsum=0)//no function declaration{cout<<"I:"<< I <<Endl; cout<<"J:"<< J <<Endl; cout<<"sum:"<< sum <<Endl;}intMain () {intA=3; intb=7; cout<<"sum (a):"<< sum (a) <<Endl; cout<<"sum (A, b):"<< sum (A, B) <<Endl; cout<<"print (A, b):"<<Endl; Print (A, b); cout<<"Print (A,b,sum (b)):"<<Endl; Print (A,b,sum (A, b)); return 0;}//Rookie, please criticize advice, code writing habits and norms and so on!!! Thank you!!!
I self-study, the textbook may be a bit old, there are problems to ask you correct!!! Thank you!!!
C + + default parameter value function