C + + has default parameters for functions---4

Source: Internet
Author: User

Original blog: Reprint please indicate the source: http://www.cnblogs.com/zxouxuewei/

1. The purpose of the default parameter

C + + can define default parameter values for functions. Typically, when a function is called, the corresponding argument is given for each parameter of the function. For example:

 void  delay (int  loops); //  function declaration  void  delay (int  loops)  function definition   { if  (100ps==0  )  return  ;  for  (int  i=0 ; i<loops,i++< Span style= "color: #000000;" );}  

Whenever you call the delay () function, you must pass a value to loops to determine the time. Sometimes, however, you need to call the delay () function repeatedly with the same arguments. C + + can define default values for parameters. If the loops in the delay () function is defined as the default value of 1000, simply change the function declaration to:

void delay (int loops=

Thus, whenever the delay () function is called, the loops is not assigned a value, and the program automatically treats it as a value of 1000. For example, call:

Delay (2500//loops set to 2500//ok:loops with default value of

Call, if the parameter is not given, the work is performed at the specified default value.
Allow the function default parameter value, is to make the programming simple, let the compiler do more check error work.

2. Declaration of default parameters
The default parameters are provided in the function declaration, and when there are declarations and definitions, the default parameters are not allowed in the definition. If a function is defined only, the default parameter can appear in the function definition. For example:

void Point (int=3,int=4// declaration gives default value void   The default values  <<x<<<<y<< Endl are not allowed in the definition ;}     

< Span class= "ff0000" >3. The order of default parameters specifies that
If there are multiple default parameters in a function, the default parameters should be progressively defined from right to left in the parameter distribution. When you call a function, you can only match parameters to the left. For example:

 void  func (int  a=1 , int  c=3 , int  d=4 ); // error  
void func (int A, int b=2 , int c=3 , int d=4 ); // ok

< Span class= "ff0000" > for the 2nd function declaration, the method that is called is defined as:

Func (ten,K,+//OK: All arguments //error are given when called: Parameter A has no default func (I2,//OK: parameter C and D default func (2, // Error: Only right-to-left order matches default

4. Default parameters and function overloading
The default parameter can combine a series of simple overloaded functions into one. For example, the following 3 overloaded functions:

void Point (int,int) {//...} void Point (int a) {return point (A,4);}void Point () {  Return Point (3,4);}

You can use the following function to override the default parameters:

void Point (int=3,int=4);

Called Point (3,4) when a "point ();" is called, which is the 3rd overloaded function declared.
When a "point (6);" is called, it is called "point (6,4);", which is the 2nd overloaded function declared.
An overloaded function that calls the 1th declaration when "Point (7,8);" is called
If a set of overloaded functions (which may have default parameters) allows the same number of calls, the two semantics of the call will be raised. For example:

voidFuncint);//one of the overloaded functionsvoidFuncint,int=4);//two of the overloaded functions with default parametersvoidFuncint=3,int=4);//three of the overloaded functions with default parametersfunc (7);//error: Which of the 3 overloaded functions is called?Func -, -)//error: Which one of the following 2 overloaded functions is called?

5. Limitations of default values
The default value can be a global variable, a global constant, or even a function. For example:

int a=1; int fun (int);int g (int   //  OK: Allow default values to function

The default value cannot be a local variable because the function call of the default parameter is determined at compile time, and the location and value of the local variable cannot be determined at compile time. For example:

 void   Fun () { int   I;  void  g (int  x=i) // error: When handling g () function declarations, I is not visible }

Summary of this chapter
As program volume and program complexity increase, the best way is to divide the program into smaller, more manageable modules, which are functions.
The function name best reflects the task that is to be done.
The function can return data to the caller, and if the function returns a value, the type of the return value must be specified before the function name, or void if the function has no return value.
The program passes the information to the function through the parameter, if the function needs to accept the parameter, must specify the name and the type to the parameter.
C + + must know the return type of the function and the number and type of arguments accepted, and if the definition of the function appears after the function call, it must be declared with the function prototype at the beginning of the program.
A local variable is defined inside a function and can only be accessed by a function that defines the variable. A global variable is a variable whose scope runs through the program at all times. Define global variables to be made at the beginning of the program and placed outside of all functions.
A static local variable is defined inside a function, but the life time is generated with the first invocation of the function and ends with the end of the program, and the static local variable can only be visible in the function that defines the variable.
The function invocation mechanism is implemented by the process of the stack operation. Functions can be called recursively. A function definition cannot be placed inside any function definition.
Inline functions are implemented to improve programming efficiency, overcoming the drawbacks of defining a defined macro with a # define.
Function overloading allows defining multiple functions with the same function name. The connector invokes the corresponding function based on the number, type, and order of arguments passed to the function. Function overloading makes programming simple, and programmers can accomplish a series of related tasks by remembering a function name.
You can specify a default parameter value by assigning a value in the function definition. The function uses the default parameter value once the program defaults to the parameter value when calling the function. Default values are not allowed to be used in the middle of a parameter. Specifying a default parameter value can make the function easier to use, and it also enhances the reusability of the function.

C + + has default parameters for functions---4

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.