C++ Default Arguments
In this page:
return_type function_name(type param1, type param2 = default_value) {
// body
}
function_name(argument); // param2 uses the default
Introduction to Default Arguments
A parameter can be given a default value in its declaration, like void greet(string name = "Guest"), so calling greet() with no argument at all automatically uses "Guest" instead of requiring the caller to supply it explicitly every time.
Example: Introduction to Default Arguments
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
void greet(std::string name = "Guest") {
// Print to the console with cout
std::cout << "Hello, " << name << std::endl;
}
// Program execution starts in main()
int main() {
greet();
greet("Alex");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Multiple Default Arguments
A function can have several default parameters at once, and C++ fills in the values you do provide from left to right — so if you supply only the first argument, every parameter after it falls back to its default.
This lets common cases stay short while still allowing full customization when needed.
Example: Multiple Default Arguments
// Include std::cout and std::cin
#include <iostream>
void configure(int width = 100, int height = 50) {
// Print to the console with cout
std::cout << width << "x" << height << std::endl;
}
// Program execution starts in main()
int main() {
configure();
configure(200);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Defaults in Function Prototypes
When a function is both declared (prototyped) and defined separately, default values must be specified only in the declaration, not repeated in the definition — putting them in both places, or only in the definition, causes a compile error.
This keeps the default value's source of truth in one place.
Example: Defaults in Function Prototypes
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
void greet(std::string name = "Guest");
// Program execution starts in main()
int main() {
greet();
// Return 0 to signal that the program finished successfully
return 0;
}
void greet(std::string name) {
// Print to the console with cout
std::cout << "Hello, " << name << std::endl;
}
Login to try C/C++/Java/PHP code in the editor
Overloading with Default Arguments
Mixing default arguments with function overloading can create genuine ambiguity for the compiler — if void show(int a, int b = 5) and void show(int a) both exist, a call to show(10) matches both, and the compiler won't know which one you meant.
It's worth checking for this kind of overlap whenever you add default values to an already-overloaded function.
Example: Overloading with Default Arguments
#include <iostream>
void show(int a, int b = 5) { std::cout << "a,b: " << a << "," << b << std::endl; }
// void show(int a) {...} // would be ambiguous with show(a, b=5) when called as show(x)
int main() {
show(10);
show(10, 20);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Position Rules
Default arguments must be grouped at the right-hand end of the parameter list — once one parameter has a default value, every parameter after it must also have one, since the compiler fills defaults from right to left based on how many arguments were actually supplied.
You can't place a required, non-default parameter after a defaulted one.
Example: Position Rules
#include <iostream>
void order(int a, int b = 2, int c = 3) {
std::cout << a << "," << b << "," << c << std::endl;
}
// void invalid(int a = 1, int b) {} // compile error: default before non-default
int main() {
order(1);
order(1, 5);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Putting a default argument before a non-default one, like
void f(int a = 1, int b), which is an error. - Repeating the default value in both the declaration and definition, which is a redefinition error.
- Adding overloads that clash with defaults, so calls become ambiguous.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- C++ Functions Introduction
- C++ Function Parameters
- C++ Multiple Function Parameters
- C++ Passing by Reference
- C++ Passing Structures to Functions
- C++ Return Values
- C++ Function Overloading
- C++ Default Arguments
- C++ Recursion
- C++ Inline Functions
- C++ Lambda Functions
- C++ Scope & Lifetime
- C++ Math Functions