C + + functions: Functions for Default parameters

Source: Internet
Author: User

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++);}

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=1000);

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 2500delay (); Ok:loops with default value of 1000

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); The default value given in the Declaration, void Point (intx,inty)//definition, is not allowed to give the default value {cout <<x<<endl;cout <<y<<endl;}

3. The order of default parameters is specified

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 b,int c=3, int d=4); errorvoid func (int a, int b=2,int c=3,int d=4); Ok

For the 2nd function declaration, the method it calls is defined as:

Func (10,15,20,30); OK: all argument func () is given when called; Error: Parameter A does not have a default value of Func (i2,12); OK: Parameters C and D default func (2,15,20); 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:

void func (int); One of the overloaded functions is void func (int,int=4); Two of the overloaded functions, with the default parameter void func (int=3,int=4); Overload function three, with default parameter func (7); Error: Which one of the 3 overloaded functions is called? Func (20,30); 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 x;fun (a)); 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 the G () function declaration, I is not visible}

Article turned from: http://blog.csdn.net/vlily/article/details/7247888

C + + functions: Functions for Default parameters

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.