Search

Meta

7.7 — Default parameters

A default parameter is a function parameter that has a default value provided to it. If the user does not supply a value for this parameter, the default value will be used. If the user does supply a value for the default parameter, the user-supplied value is used.

Consider the following program:

This program produces the following output:

1st value: 1
2nd value: 10
1st value: 3
2nd value: 4

In the first function call, the caller did not supply an argument for nValue2, so the function used the default value of 10. In the second call, the caller did supply a value for nValue2, so the user-supplied value was used.

Default parameters are an excellent option when the function needs a value that the user may or may not want to override. For example, here are a few function prototypes for which default parameters might be commonly used:

A function can have multiple default parameters:

Given the following function calls:

The following output is produced:

Values: 1 2 3
Values: 1 2 30
Values: 1 20 30
Values: 10 20 30

Note that it is impossible to supply a user-defined value for nValue3 without also supplying a value for nValue1 and nValue2. This is because C++ does not support a function call such as PrintValues(,,3). This has two major consequences:

1) All default parameters must be the rightmost parameters. The following is not allowed:

2) The leftmost default parameter should be the one most likely to be changed by the user.

Default parameters and function overloading

Functions with default parameters may be overloaded. For example, the following is allowed:

If there user were to call Print(), it would resolve to Print(' '), which would print a space.

However, it is important to note that default parameters do NOT count towards the parameters that make the function unique. Consequently, the following is not allowed:

If the caller were to call PrintValues(10), the compiler would not be able to disambiguate whether the user wanted PrintValues(int) or PrintValues(int, 20) with the default value.

7.8 -- Function Pointers
Index
7.6 -- Function overloading

11 comments to 7.7 — Default parameters

  • Pathik

    how do you get a random value in C++?
    For example :

    You see, I want y to be a random number when program starts up.

  • Ericc

    u forgot ; in the Prototype

  • simon1122

    //Function
    #include
    using namespace std;

    void printvalues(int nvalue1, int nvalue2=10 );

    int main()
    {

    printvalues(1);
    printvalues(1,3);

    /*scaffolding code for testing purpose*/
    cin.ignore(256, ‘\n’);
    cout<< "press Enter to continue.." << endl;
    cin.get();

    return 0;
    }

    void printvalues(int nvalue1, int nvalue2 = 10 )
    {

    cout<<"1st : "<< nvalue1<< endl;
    cout<<"2st : "<< nvalue2<< endl;

    }

  • simon1122

    hi

    can any one tell me what is wrong with this one??

    thak you

    • Anshuman

      you must not use specify the default value both in function prototype and definition

  • dave

    I must say this site is awesome and clean looking..specially how the code is displayed….looks neat! keep up good work and i am doing last min craming when i googled default params…so wish me luck! :)

  • otocan

    Hi Alex

    Shouldn’t all the examples of pointers to chars in this chapter e.g.

    char *strValue

    be instead

    char *pszValue?

    Cheers

    Kevin

  • Sphingine

    Can i give d definition as:

    void printvalues(int nvalue1=0, int nvalue2=2)
    {

    cout<<"1st : "<< nvalue1<< endl;
    cout<<"2st : "<< nvalue2<< endl;

    }

    and call the function as:

    printvalues(,3);

  • kekie

    Shouldn’t they be called ‘Default arguments’?

Leave a Comment

  

  

  

three × 3 =

Put C++ code inside [code][/code] tags to use the syntax highlighter