Default Arguments
in C++
Learn how to simplify function calls by assigning fallback values to parameters, making your code cleaner and more flexible.
What Are Default Arguments?
A default argument is a value assigned to a function parameter in the function declaration. If the caller does not provide a value for that parameter, the compiler automatically uses the default.
void greet(string name = "World"); greet("Alice"); // prints "Hello, Alice" greet(); // prints "Hello, World"
Why Use Default Arguments?
How the Compiler Fills the Gap
The Core Rule: Right-to-Left
Default arguments must be assigned from the rightmost parameter toward the left, with no gaps. Once a parameter has a default, every parameter to its right must also have one.
// VALID: defaults fill from right void draw(int x, int y, int w = 100, int h = 100); // INVALID: gap in defaults void draw(int x, int y = 0, int w, int h = 100); // ERROR!
Declaration vs Definition
In C++, default values are typically specified in the function declaration (the prototype), not in the definition. Specifying them in both is a compiler error.
// header.h (declaration) — defaults go here void log(string msg, int level = 1); // source.cpp (definition) — NO defaults here void log(string msg, int level) { // implementation... }
Defaults vs Function Overloading
| Default Arguments | Overloading | |
|---|---|---|
| Functions needed | 1 | Multiple |
| Different logic per variant? | No: same body | Yes: each has its own body |
| Parameter types can differ? | No | Yes |
| Best when | Same logic, fewer args | Different behavior needed |
How to Apply Default Arguments
Example 1: Power Function
Write a function that raises a base to an exponent, defaulting to squaring.
double power(double base, int exp = 2) { double result = 1; for (int i = 0; i < exp; i++) result *= base; return result; } cout << power(5); // 25 (5^2, default exp) cout << power(5, 3); // 125 (5^3, exp overridden)
Example 2: Print Separator
A utility that prints a separator line with configurable character, length, and newlines.
void separator(char ch = '-', int len = 40, bool newline = true) { for (int i = 0; i < len; i++) cout << ch; if (newline) cout << '\n'; } separator(); // ----...---- (40 dashes + newline) separator('='); // ====...==== (40 equals + newline) separator('*', 20); // **...** (20 stars + newline) separator('*', 20, false); // **...** (20 stars, no newline)
Example 3: Grade Calculator
string letterGrade(double score, double aThresh = 90.0, double bThresh = 80.0, double cThresh = 70.0) { if (score >= aThresh) return "A"; if (score >= bThresh) return "B"; if (score >= cThresh) return "C"; return "F"; } cout << letterGrade(85); // "B" (standard scale) cout << letterGrade(85, 85.0); // "A" (curved: A at 85+)
You Cannot Skip Middle Arguments
Unlike some other languages, C++ does not allow you to skip a defaulted parameter and supply a later one. Arguments are always matched left to right.
void setup(int width = 800, int height = 600, bool fullscreen = false); // Want fullscreen but keep default width & height? setup(true); // WRONG! true converts to int 1 (width=1) setup(800, 600, true); // CORRECT: must supply all preceding args
Defaults Are Resolved at Compile Time
Defaults Can Be Expressions
Default values are not limited to literals. They can be variables, function calls, or complex expressions: anything that can be evaluated at the call site.
int screenWidth = 1920; void resize(int w = screenWidth, int h = screenWidth * 9 / 16) { // w defaults to 1920, h defaults to 1080 } // If screenWidth changes before the call, // the new value is used! screenWidth = 2560; resize(); // w=2560, h=1440
Common Mistakes to Avoid
Quick Reference Summary
| Rule | Detail |
|---|---|
| Placement | In the declaration (prototype), not definition |
| Order | Right-to-left: no gaps allowed |
| Matching | Arguments fill left-to-right: can't skip |
| Value types | Literals, constants, variables, expressions |
| Evaluation | At each call site, at compile time |
| Overload conflict | Avoid defaults + overloads with same call signature |
Question 1 of 5
Which declaration correctly uses default arguments?
Question 2 of 5
Given: void print(string s, int n = 1, char end = '\n');
What does print("Hi", 3) resolve to?
Question 3 of 5
Where should default argument values be specified when using separate header and source files?
Question 4 of 5
What happens if you define both:
void foo(int a, int b = 5); and void foo(int a);
then call foo(10)?
Question 5 of 5
Given: void draw(int x, int y, int w = 100, int h = 100);
Which call is invalid?