A default argument is a default value provided for a function parameter. If the user does not supply an explicit argument for a parameter with a default argument, the default value will be used. If the user does supply an argument for the parameter, the user-supplied argument is used.
Because the user can choose whether to supply a specific argument value, or use the default, a parameter with a default value provided is often called an optional parameter.
Consider the following program:
1 2 3 4 5 6 7 8 9 10 11 |
void printValues(int x, int y=10) // 10 is the default argument, y is now an optional parameter { std::cout << "x: " << x << '\n'; std::cout << "y: " << y << '\n'; } int main() { printValues(1); // y will use default argument 10 printValues(3, 4); // y will use user-supplied argument 4 } |
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 arguments 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 arguments might be commonly used:
1 2 3 |
void openLogFile(std::string filename="default.log"); int rollDie(int sides=6); void printStringInColor(std::string str, Color color=COLOR_BLACK); // Color is an enum |
Multiple default arguments
A function can have multiple default arguments:
1 2 3 4 |
void printValues(int x=10, int y=20, int z=30) { std::cout << "Values: " << x << " " << y << " " << z << '\n'; } |
Given the following function calls:
1 2 3 4 |
printValues(1, 2, 3); printValues(1, 2); printValues(1); printValues(); |
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 arguments must be for the rightmost parameters. The following is not allowed:
1 |
void printValue(int x=10, int y); // not allowed |
2) If more than one default argument exists, the leftmost default argument should be the one most likely to be explicitly set by the user.
Default arguments can only be declared once
Once declared, a default argument can not be redeclared. 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.
1 2 3 4 5 6 7 |
void printValues(int x, int y=10); void printValues(int x, int y=10) // error: redefinition of default argument { std::cout << "x: " << x << '\n'; std::cout << "y: " << y << '\n'; } |
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 (particularly if it’s in a header file).
in foo.h:
1 2 3 4 |
#ifndef FOO_H #define FOO_H void printValues(int x, int y=10); #endif |
in main.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include "foo.h" #include <iostream> void printValues(int x, int y) { std::cout << "x: " << x << '\n'; std::cout << "y: " << y << '\n'; } int main() { printValues(5); return 0; } |
Note that in the above example, we’re able to use the default argument for function printValues() because the main.cpp #includes foo.h, which has the forward declaration that defines the default argument.
Rule
If the function has a forward declaration, put the default argument there. Otherwise, put them in the function definition.
Default arguments and function overloading
Functions with default arguments may be overloaded. For example, the following is allowed:
1 2 |
void print(std::string string); void print(char ch=' '); |
If the user were to call print()
, it would resolve to print(' ')
, which would print a space.
However, it is important to note that optional parameters do NOT count towards the parameters that make the function unique. Consequently, the following is not allowed:
1 2 |
void printValues(int x); void printValues(int x, int y=20); |
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.
Summary
Default arguments 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.
![]() |
![]() |
![]() |