1. Intro
A default argument lets you assign a fallback value to a function parameter. If the caller omits that argument when calling the function, the default value is used automatically. This is useful when a parameter usually has one common value but occasionally needs to be overridden.
Cricket analogy: A default argument is like a bowler's standard field setting for a new batsman — used automatically unless the captain explicitly calls for a different, specific field for a known aggressive player like Jos Buttler.
2. Syntax
returnType functionName(type param1, type param2 = defaultValue) {
// function body
}
// Example
void greet(string name = "Guest") {
cout << "Hello, " << name << "!" << endl;
}3. Explanation
You assign a default value to a parameter using = value in the function's declaration or definition. When the caller provides an argument for that parameter, the supplied value is used instead of the default; when the caller omits it, the default kicks in. The critical rule is that default arguments must be the trailing (rightmost) parameters — once one parameter has a default value, every parameter after it must also have a default value. This is because C++ matches arguments to parameters from left to right, so you cannot skip an earlier parameter while supplying a later one.
Cricket analogy: You set a default with = value like pre-assigning a standard net bowler for practice unless a specific one is named; the rule that defaults must be trailing mirrors batting order — you can't skip announcing opener 2 while naming opener 1, since positions are filled left to right.
You cannot write void f(int a = 5, int b) — this is a compile-time error because a defaulted parameter (a) is followed by a non-defaulted one (b). Once you default a parameter, every parameter after it must also have a default.
4. Example
#include <iostream>
using namespace std;
int power(int base, int exponent = 2) {
int result = 1;
for (int i = 0; i < exponent; i++) {
result *= base;
}
return result;
}
int main() {
cout << "power(3): " << power(3) << endl; // uses default exponent = 2
cout << "power(2, 5): " << power(2, 5) << endl; // overrides default
return 0;
}5. Output
power(3): 9
power(2, 5): 326. Key Takeaways
- A default argument provides a fallback value used when the caller omits that argument.
- Default values are specified with '= value' in the function signature.
- Default arguments must be the trailing (rightmost) parameters in the parameter list.
- A caller-supplied argument always overrides the parameter's default value.
Practice what you learned
1. What is a default argument in C++?
2. Which of the following function signatures is INVALID in C++?
3. Given `void greet(string name = "Guest")`, what does calling `greet();` print?
4. If a caller explicitly supplies a value for a parameter with a default, which value is used?
5. Where must default arguments appear in a function's parameter list?
Was this page helpful?
You May Also Like
Functions in C++
Learn what a function is in C++, why programs are broken into functions, and how reusability, modularity, and readability improve with them.
Function Parameters and Arguments in C++
Learn the difference between function parameters and arguments in C++ and understand pass-by-value semantics with a hands-on example.
Function Overloading in C++
Learn how C++ resolves multiple same-named functions at compile time based on parameter lists, and why return type alone cannot distinguish overloads.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics