Parsing the default parameters and placeholder parameters of C + + functions and expanding _c language compared to C language

Source: Internet
Author: User

You can use placeholder parameters in conjunction with default parameters
Significance:
Leave clues for future expansion of the program
Possible nonstandard writing in compatible C language programs

C + + can declare placeholder parameters, which are generally used for program extensions and compatible int func for C code 
(int A, int b, int = 0) {return 
  a + b; 
} 
void Main () 
{ 
  //if the default and placeholder parameters are together, it can be invoked 
  func (1, 2); 
  Func (1, 2, 3); 
  System ("pause"); 
} 


function Extension of the default parameter to C
1.c++ can provide a default value for a parameter when the function is declared.
When a function call does not specify a value for this parameter, the compiler automatically replaces the default value with the

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

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

Default parameter 
void printab (int x = 3) 
{ 
  printf ("x:%d\n", x); 
} 
 
In the default parameter rule, if the default parameter appears, then the right must have a default parameter 
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; 


function extension of the default parameter to C
1.c++ can provide a default value for a parameter when the function is declared, and when the function is called without specifying the value of the parameter, the compiler automatically replaces the default value:

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

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

Default parameter 
void printab (int x = 3) 
{ 
  printf ("x:%d\n", x); 
} 
 
In the default parameter rule, if the default parameter appears, then the right must have a default parameter 
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 Article

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.