Language Selector

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
7.6 -- Function overloading

13 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

    using namespace std;

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

    int main()


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

    return 0;

    void printvalues(int nvalue1, int nvalue2 = 10 )

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


  • simon1122


    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?



  • 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:


  • kekie

    Shouldn’t they be called ‘Default arguments’?

  • Alex, is there any way to forward declare a function that has default pararmeters in it…?

    #include <iostream>

    void val(int, int);

    int main()
        int value = 10;
        return 0;

    void val(int nval, int val2 = 20)
        std::cout << nval << "\n";
        std::cout << val2 << "\n";

    This program gives an error at line 8:

    "Fire.cpp|8|error: too few arguments to function ‘void val(int, int)’|"

    and if I remove an int, compiler says that it is unable to find a function with only 1 int parameter.

    • Alex

      Put the default parameter in the forward declaration:

Leave a Comment




3 × one =

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