Default arguments
A default argument gives a parameter a fallback value that the compiler fills in when the caller omits it. Only trailing parameters can have defaults, and the default belongs in the declaration the caller can see.
Detailed Explanation
Suppose you are writing a database client. Most callers want connect("db.internal") and nothing more. A few need a different port. One test harness needs a shorter timeout. Default arguments let a single function serve all three callers: void connect(std::string host, int port = 5432, int timeoutMs = 3000);.
The alternative, and what C forces you into, is a family of functions with different names, or a sentinel value like -1 meaning "pick something sensible". Both push the decision into the function body and into the caller's memory. In C++ the alternative is a set of overloads that forward to each other. That works, but for four optional parameters you are writing five functions that all say the same thing.
Only trailing parameters can have defaults. Arguments are matched to parameters by position, so if port had a default and timeoutMs did not, the compiler could not tell whether connect("db", 500) meant a port or a timeout. Once one parameter has a default, every parameter to its right must have one too. There is no way to skip a middle argument.
Write the default once, in the declaration the caller sees. For a function declared in a header and defined in a .cpp, that means the header. Repeating the same default in the definition is ill-formed. The rule that matters in practice is that the default is resolved from whatever declaration is visible at the call site, so two translation units that see different declarations will behave differently, and nothing warns you.
The default expression is evaluated at the call site, on every call, not once at program start. void log(std::string msg, TimePoint t = now()); calls now() each time. Names in the expression are looked up where the declaration appears, not where the call appears.
Now the trap. Default arguments are resolved statically, from the static type of the expression you call through. Virtual dispatch picks the override at runtime, but the default value was already baked in. So a Base* pointing at a Derived object runs Derived::draw with Base's default. The override's own default is silently ignored. The fix is to never put a default argument on a virtual function. Put it on a non-virtual public wrapper that forwards to a virtual implementation.
Choose overloads instead when the shortened call needs genuinely different logic, when you want different parameter types, or when you need a stable ABI. Choose defaults when the extra parameters are true refinements of one behavior; you keep one function, one address, and one body to test. For more than three or four optional settings, an options struct filled with C++20 designated initializers reads better than either.
Code Examples
#include <string>
#include <iostream>
// Declaration (normally in the header) carries the defaults.
void connect(const std::string& host, int port = 5432, int timeoutMs = 3000);
// Definition (normally in the .cpp) must NOT repeat them.
void connect(const std::string& host, int port, int timeoutMs) {
std::cout << host << ':' << port << " t=" << timeoutMs << '\n';
}
int main() {
connect("db.internal"); // prints: db.internal:5432 t=3000
connect("db.internal", 6543); // prints: db.internal:6543 t=3000
connect("db.internal", 6543, 500); // prints: db.internal:6543 t=500
// connect("db.internal", 500); // means PORT 500, not timeout 500
}#include <iostream>
struct Shape {
virtual void draw(int width = 1) const = 0;
virtual ~Shape() = default;
};
struct Circle : Shape {
void draw(int width = 10) const override { // this default is never used
std::cout << "circle width " << width << '\n';
}
};
int main() {
Circle c;
const Shape& s = c;
s.draw(); // prints: circle width 1 -- Shape's default, Circle's body
c.draw(); // prints: circle width 10
}Common Gotchas
Default arguments on virtual functions lie
the default comes from the static type of the pointer or reference, while the body comes from the dynamic type. You get the base's value inside the derived function. Keep virtual functions defaulted-argument-free and expose a non-virtual wrapper that supplies the default.
The default is baked into every call site
changing `int timeoutMs = 3000` to `= 5000` in a header does nothing for callers you did not recompile. In a shared library this is an ABI-visible change, not an implementation detail.
Two declarations, two behaviors
if one translation unit sees a declaration with a default and another sees one without, or with a different value, the same call spelled the same way does different things. Declare the function in exactly one header and include it everywhere.
The expression runs on every call
a default of `now()` or `std::vector<int>{}` is constructed fresh per call, at the caller's expense. That is usually what you want, but it is not a free constant.