C++ Default Arguments
In this page:
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 <iostream>
#include <string>
void greet(std::string name = "Guest") {
std::cout << "Hello, " << name << std::endl;
}
int main() {
greet();
greet("Alex");
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 <iostream>
void configure(int width = 100, int height = 50) {
std::cout << width << "x" << height << std::endl;
}
int main() {
configure();
configure(200);
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 <iostream>
#include <string>
void greet(std::string name = "Guest");
int main() {
greet();
return 0;
}
void greet(std::string name) {
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
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