Parsing the default and placeholder parameters of the C++ function and extending the C language

  • 2020-05-09 18:57:12
  • OfStack

Placeholder parameters can be used in combination with default parameters
Meaning:
Leave clues for future extensions
Compatible with non-standard writing in C language programs


//C++ You can declare placeholder parameters, placeholder parameters 1 It is used for program extension and pair C Code compatibility  
int func(int a, int b, int = 0) 
{ 
  return a + b; 
} 
void main() 
{ 
  // If the default parameter and the placeholder parameter are in 1 Up, can call up  
  func(1, 2); 
  func(1, 2, 3); 
  system("pause"); 
} 


The default parameter extends the C function
1.C++ can provide a default value for parameters when functions are declared,
When the value of this parameter is not specified when the function is called, the compiler automatically replaces it with the default value


void myPrint(int x = 3) 
{ 
  printf("x:%d", x); 
} 

2. Rules for function default parameters
Only parameters in the later part of the parameter list can provide default parameter values. Once the default parameter value is used in a function call, the default parameter value must be used for all parameters after this parameter:


// The default parameters  
void printAB(int x = 3) 
{ 
  printf("x:%d\n", x); 
} 
 
// In the default parameter rule   , if the default parameters appear, then the ones on the right must have default parameters  
void printABC(int a, int b, int x = 3, int y=4, int z = 5) 
{ 
  printf("x:%d\n", x); 
} 
int main(int argc, char *argv[]) 
{ 
  printAB(2); 
  printAB(); 
  system("pause"); 
  return 0; 
} 

 


The default parameter extends the C function
1.C++ can provide a default value for the parameter when the function is declared. If the value of this parameter is not specified when the function is called, the compiler will automatically replace it with the default value:


void myPrint(int x = 3) 
{ 
  printf("x:%d", x); 
} 

2. Rules for default parameters of functions:
Only parameters in the later part of the parameter list can provide default parameter values.
Once you start using the default parameter value in a function call, you must use the default parameter value for all parameters after that parameter.


// The default parameters  
void printAB(int x = 3) 
{ 
  printf("x:%d\n", x); 
} 
 
// In the default parameter rule   , if the default parameters appear, then the ones on the right must have default parameters  
void printABC(int a, int b, int x = 3, int y=4, int z = 5) 
{ 
  printf("x:%d\n", x); 
} 
int main(int argc, char *argv[]) 
{ 
  printAB(2); 
  printAB(); 
  system("pause"); 
  return 0; 
} 


Related articles: