Module 3 · Functions and References — topic 4 of 5
Default Arguments
Give parameters default values so callers can omit them. Learn the rules for where defaults go and how they interact with overloading.
Learning objectives
- Declare functions with default arguments
- Follow the right-to-left rule
- Place defaults in declarations, not definitions
Definition
A default argument is a value supplied in a function declaration that is used when the caller omits the corresponding trailing argument.
Introduction
Many functions have parameters that are usually the same: a separator of ", ", a precision of 2, a starting index of 0. Default arguments let callers skip them while still allowing them to be overridden.
Why it matters
Defaults keep simple calls short while keeping the function flexible.
They often replace a family of overloads that only differ by missing arguments.
Detailed explanation
Defaults are given right to left: once a parameter has a default, every parameter after it must too. void f(int a, int b = 2, int c = 3); is valid; void f(int a = 1, int b); is not.
Callers can omit only trailing arguments: f(1), f(1, 5), f(1, 5, 9). You cannot skip b and give c.
Specify defaults once, in the declaration (usually the header). Repeating them in the definition is an error.
The default is substituted at the call site at compile time, so changing a default requires recompiling callers.
Syntax
- Defaults can be expressions, including other constants.
- Avoid defaults that make overloads ambiguous.
- Named parameters don't exist in C++; order matters.
Code examples
A greeting with a default
Hello, Asha! Namaste, Ravi!
Several defaults
5 10 30
More examples
Formatting with defaults
9.50, 120.00, 3.14 10, 120, 3 9.5 | 120.0 | 3.1
Step-by-step explanation
std::ostringstreambuilds a string with stream operators.std::fixedandsetprecisioncontrol decimal places.- The first call uses both defaults; the second overrides precision; the third overrides both.
- 9.5 with 0 decimals rounds to 10.
Expected output
Running “Formatting with defaults” prints exactly:
Practical applications
- Logging functions default the log level.
- Constructors default optional settings.
- Formatting utilities default separators and precision.
Best practices
- Put defaults in the header declaration.
- Use defaults for genuinely common values.
- Prefer separate named functions if the meaning changes.
Common mistakes
Default before a non-default parameter
Avoid
Prefer
Defaults must be trailing.
Repeating the default in the definition
Avoid
Prefer
A default may be specified only once in a scope.
Important notes
- Virtual functions use the default argument of the static type at the call site, which can surprise; avoid defaults on virtual functions.
Key takeaways
- Defaults fill omitted trailing arguments.
- Declare them once, in the declaration.
- Callers cannot skip middle arguments.
Try it yourself
- 1Write
repeat(const std::string& s, int times = 2, char sep = ' '). - 2Replace three overloads of
print_tablewith one function using defaults.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
4 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhy is `void draw(int x = 0, int y);` invalid?
- ModerateWhat is printed?