7.7 — Default parameters

A default parameter (also called an optional parameter or a default argument) 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 instead of the default value.

Consider the following program:

This program produces the following output:

x: 1
y: 10
x: 3
y: 4

In the first function call, the caller did not supply an argument for y, so the function used the default value of 10. In the second call, the caller did supply a value for y, 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:

Multiple default parameters

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 an argument for parameter z without also supplying arguments for parameters x and y. This is because C++ does not support a function call syntax such as printValues(,,3). This has two major consequences:

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

2) If more than one default parameter exists, the leftmost default parameter should be the one most likely to be explicitly set by the user.

Default parameters can only be declared once

Once declared, a default parameter can not be redeclared. That means for a function with a forward declaration and a function definition, the default parameter can be declared in either the forward declaration or the function definition, but not both.

Best practice is to declare the default parameter in the forward declaration and not in the function definition, as the forward declaration is more likely to be seen by other files (particularly if it’s in a header file).

in foo.h:

in main.cpp:

Note that in the above example, we’re able to use the default parameter for function printValues() because the main.cpp #includes foo.h, which has the forward declaration that defines the default parameter.

Rule: If the function has a forward declaration, put the default parameters there. Otherwise, put them in the function definition.

Default parameters and function overloading

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

If the 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.


Default parameters provide a useful mechanism to specify parameters that the user may optionally provide values for. They are frequently used in C++, and you’ll see them a lot in future lessons.

7.8 -- Function Pointers
7.6 -- Function overloading

22 comments to 7.7 — Default parameters

  • nikos-13

    "void printStringInColor(std::string, Color color=COLOR_BLACK); // Color is an enum"
    You forgot the parameter’s, of type std::string, name.

  • "Note that it is impossible to supply a user-defined value for z without also supplying a value for x and y."
    This function refers to printValues(int x, int y, int z) so I think the sentence should be:
    "Note that it is impossible to supply a user-defined value for x without also supplying a value for y and z."
    Thanks as always.

    • Alex

      Lesson fixed. Thanks!

      • Kess

        Actually, I believe it was correct in it’s former version, as in order to provide the value only for x you could just write printValues(3);
        and the function would be called with 3, 20 and 30 as its arguments (20 and 30 being the default values for y and z)

        "Note that it is impossible to supply a user-defined value for x without also supplying a value for y and z."
        Would mean that I would have to write printValues(3, 20, 30); While possible, it’s easier and still correct to write printValues(3);

        "Note that it is impossible to supply a user-defined value for z without also supplying a value for x and y."
        Would mean that I cannot write printValues(,,3); which is the intended outcome of that particular part of the lesson. Also, the next sentence in the explanation makes it clear that the intention was to point out that you cannot set z while leaving x and y to their default values without at least explicitly set them (to their default value, if you need them to be that way).

        Thanks for these top notch lessons ๐Ÿ˜‰
        Have a good day

  • Akshay Chavan

    In foo.h

    Shouldn’t it be

  • Matt

    At the end of section "Default parameters can only be declared once", you wrote:

    "Rule: If the function has a forward declaration, put the default parameters there. Otherwise, put them on the function definition."

    Did you mean to write "on", or did you mean "in"?

  • Kattencrack Kledge

    Little typo on one of the function prototypes:

    I’m pretty sure you meant rollDice than rollDie XD

    • Alex

      Nope. Die is actually the singular of dice, and in this case, we are just rolling one die, not multiple dice. Though enough people don’t know this that I suspect it will be acceptable to use dice in both situations in the near future (if we haven’t hit that point already).

  • Jim

    This code uses the same name and parameters except one has a capital V and the other uses a lower case v.
    Why wouldn’t the compiler catch this? Or is this a typo?
    void printvalues(int x, int y=10);

    void printValues(int x, int y=10) // error: redefinition of default parameter
        std::cout << "x: " << x << ‘\n’;
        std::cout << "y: " << y << ‘\n’;

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

  • kekie

    Shouldn’t they be called ‘Default arguments’?

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


  • 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! ๐Ÿ™‚

Leave a Comment

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