A default argument is a default value provided for a function parameter. For example:
When making a function call, the caller can optionally provide an argument for any function parameter that has a default argument. If the caller provides an argument, the value of the argument in the function call is used. If the caller does not provide an argument, the value of the default argument is used.
Consider the following program:
This program produces the following output:
x: 1 y: 2 x: 3 y: 4
In the first function call, the caller supplied explicit arguments for both parameters, so those argument values are used. In the second function call, the caller omitted the second argument, so the default value of 4
was used.
Note that you must use the equals sign to specify a default argument. Using parenthesis or brace initialization won’t work:
Perhaps surprisingly, default arguments are handled by the compiler at the call site. In the above example, when the compiler sees print(3)
, it will rewrite this function call as print(3, 4)
, so that the number of arguments matches the number of parameters. The rewritten function call then works as per usual.
Key insight
Default arguments are inserted by the compiler at site of the function call.
Default arguments are frequently used in C++, and you’ll see them a lot in code you encounter (and in future lessons).
When to use default arguments
Default arguments are an excellent option when a function needs a value that has a reasonable default value, but for which you want to let the caller override if they wish.
For example, here are a couple of function prototypes for which default arguments might be commonly used:
Author’s note
Because the user can choose whether to supply a specific argument value or use the default value, a parameter with a default value provided is sometimes called an optional parameter. However, the term optional parameter is also used to refer to several other types of parameters (including parameters passed by address, and parameters using std::optional
), so we recommend avoiding this term.
Default arguments are also useful in cases where we need to add a new parameter to an existing function. If we add a new parameter without a default argument, it will break all existing function calls (which aren’t supplying an argument for that parameter). This can result in a lot of updating of existing function calls (and may not even be possible if you don’t own the calling code). However, if we add a new parameter with a default argument instead, all existing function calls will still work (as they will use the default argument for the parameter), while still letting new calls to the function specify an explicit argument if desired.
Multiple default arguments
A function can have multiple parameters with default arguments:
The following output is produced:
Values: 1 2 3 Values: 1 2 30 Values: 1 20 30 Values: 10 20 30
C++ does not (as of C++23) support a function call syntax such as print(,,3)
(as a way to provide an explicit value for z
while using the default arguments for x
and y
. This has three major consequences:
- In a function call, any explicitly provided arguments must be the leftmost arguments (arguments with defaults cannot be skipped).
For example:
- If a parameter is given a default argument, all subsequent parameters (to the right) must also be given default arguments.
The following is not allowed:
Rule
If a parameter is given a default argument, all subsequent parameters (to the right) must also be given default arguments.
- If more than one parameter has a default argument, the leftmost parameter should be the one most likely to be explicitly set by the user.
Default arguments can not be redeclared, and must be declared before use
Once declared, a default argument can not be redeclared in the same translation unit. That means for a function with a forward declaration and a function definition, the default argument can be declared in either the forward declaration or the function definition, but not both.
The default argument must also be declared in the translation unit before it can be used:
The best practice is to declare the default argument in the forward declaration and not in the function definition, as the forward declaration is more likely to be seen by other files and included before use (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 argument for function print()
because main.cpp #includes foo.h, which has the forward declaration that defines the default argument.
Best practice
If the function has a forward declaration (especially one in a header file), put the default argument there. Otherwise, put the default argument in the function definition.
Default arguments and function overloading
Functions with default arguments may be overloaded. For example, the following is allowed:
The function call to print()
actually calls print(char)
, which acts as if the user had explicitly called print(' ')
.
Now consider this case:
Default values are not part of a function’s signature, so these function declarations are differentiated overloads.
Related content
We discuss function overload differentiation in lesson 11.2 -- Function overload differentiation
Default arguments can lead to ambiguous matches
Default arguments can easily lead to ambiguous function calls:
In this example, the compiler can’t tell whether foo()
should resolve to foo(0)
or foo(0.0)
.
Here’s a slightly more complex example:
For the call print(1)
, the compiler is unable to tell whether this resolve to print(int)
, print(int, int)
, or print(int, double)
.
In the case where we mean to call print(int, int)
or print(int, double)
we can always explicitly specify the second parameter. But what if we want to call print(int)
? It’s not obvious how we can do so.
Default arguments don’t work for functions called through function pointers Advanced
We cover this topic in lesson 20.1 -- Function Pointers. Because default arguments are not considered using this method, this also provides a workaround to call a function that would otherwise be ambiguous due to default arguments.