Default parameters in C + +

Source: Internet
Author: User

1. The default value of the parameter may be declared in the function prototype, and the default value is a value that is used when no parameter value is supplied.

such as function prototype long myFunction (int x=50);

The prototype indicates a parameter default value of 50, which is not affected by the declaration of parameter values in the prototype and function definitions, which can also be used:

Long myFunction (int x) {function definition};

If the function MyFunction () is called without a given argument, then the default value is used X=50

2, parameter Default value NOTE: If a parameter does not have a default value, then all of its previous parameters must not use the default value.

such as long myFunction (int param1,int param2,int param3), the PARAM3 default value can be given only when the default value is used for param2, and only if param3, Param2 is given a default value to give the param1 default value. (The default value is given in the right-to-left order, because the rule: the parameter sets the default value from right to left, and the argument is assigned to the parameter from left to right )

3. Use an instance of the default parameter (21 days to learn C + + P846.7): Write a function to calculate the cube volume.

#include <iostream>using namespacestd;//int areacube (int length,int width=25,int height=1);intCubevolume (intLengthintWidth= -,intheight=1)///Note that if set to
int cubevolume (int length=25,int width=1,int height)
You will get an error because the default parameter is set from right to left in the formal parameterintMain () {intLength= -, width= -, height=Ten; intvolume; Volume=Cubevolume (length,width,height); cout<<"firt cube equals:"<<volume; Volume = Cubevolume (length,width);//What default parameter you want to use, it is OK to call the function directly without calling this parameter cout<<"second cube equals:"<<volume; Volume=cubevolume (length); cout<<"third cube equals:"<<volume; System ("PAUSE"); return 0;}intCubevolume (intLengthintWidthintheight) { return(length*width*height);}

Default parameters in C + +

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.