Skip to content

Module 3 · Functions and References — topic 4 of 5

Default Arguments

Give parameters default values so callers can omit them. Learn the rules for where defaults go and how they interact with overloading.

Beginner 10 min read·Lesson 11 of 40 in C++ Programming

Learning objectives

  • Declare functions with default arguments
  • Follow the right-to-left rule
  • Place defaults in declarations, not definitions

Definition

A default argument is a value supplied in a function declaration that is used when the caller omits the corresponding trailing argument.

Introduction

Many functions have parameters that are usually the same: a separator of ", ", a precision of 2, a starting index of 0. Default arguments let callers skip them while still allowing them to be overridden.

Why it matters

Defaults keep simple calls short while keeping the function flexible.

They often replace a family of overloads that only differ by missing arguments.

Detailed explanation

Defaults are given right to left: once a parameter has a default, every parameter after it must too. void f(int a, int b = 2, int c = 3); is valid; void f(int a = 1, int b); is not.

Callers can omit only trailing arguments: f(1), f(1, 5), f(1, 5, 9). You cannot skip b and give c.

Specify defaults once, in the declaration (usually the header). Repeating them in the definition is an error.

The default is substituted at the call site at compile time, so changing a default requires recompiling callers.

Syntax

C++
// header
std::string join(const std::vector<std::string>& parts, const std::string& sep = ", ");
// source
std::string join(const std::vector<std::string>& parts, const std::string& sep) { /* ... */ }
  • Defaults can be expressions, including other constants.
  • Avoid defaults that make overloads ambiguous.
  • Named parameters don't exist in C++; order matters.

Code examples

A greeting with a default

C++
#include <iostream>
#include <string>
void greet(const std::string& name, const std::string& greeting = "Hello") {
std::cout << greeting << ", " << name << "!\n";
}
int main() {
greet("Asha");
greet("Ravi", "Namaste");
return 0;
}
Output
Hello, Asha!
Namaste, Ravi!

Several defaults

C++
#include <iostream>
int volume(int l, int w = 1, int h = 1) { return l * w * h; }
int main() {
std::cout << volume(5) << ' ' << volume(5, 2) << ' ' << volume(5, 2, 3) << '\n';
return 0;
}
Output
5 10 30

More examples

Formatting with defaults

C++
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
std::string format_list(const std::vector<double>& v, int precision = 2, const std::string& sep = ", ") {
std::ostringstream out;
out << std::fixed << std::setprecision(precision);
for (std::size_t i = 0; i < v.size(); ++i) {
if (i) out << sep;
out << v[i];
}
return out.str();
}
int main() {
std::vector<double> prices{9.5, 120.0, 3.14159};
std::cout << format_list(prices) << '\n';
std::cout << format_list(prices, 0) << '\n';
std::cout << format_list(prices, 1, " | ") << '\n';
return 0;
}
Output
9.50, 120.00, 3.14
10, 120, 3
9.5 | 120.0 | 3.1

Step-by-step explanation

  • std::ostringstream builds a string with stream operators.
  • std::fixed and setprecision control decimal places.
  • The first call uses both defaults; the second overrides precision; the third overrides both.
  • 9.5 with 0 decimals rounds to 10.

Expected output

Running “Formatting with defaults” prints exactly:

TextOutput
9.50, 120.00, 3.14
10, 120, 3
9.5 | 120.0 | 3.1

Practical applications

  • Logging functions default the log level.
  • Constructors default optional settings.
  • Formatting utilities default separators and precision.

Best practices

  • Put defaults in the header declaration.
  • Use defaults for genuinely common values.
  • Prefer separate named functions if the meaning changes.

Common mistakes

Default before a non-default parameter

Avoid

void draw(int x = 0, int y);

Prefer

void draw(int x, int y = 0);

Defaults must be trailing.

Repeating the default in the definition

Avoid

void log(int level = 1);
void log(int level = 1) { }

Prefer

void log(int level = 1);
void log(int level) { }

A default may be specified only once in a scope.

Important notes

  • Virtual functions use the default argument of the static type at the call site, which can surprise; avoid defaults on virtual functions.

Key takeaways

  • Defaults fill omitted trailing arguments.
  • Declare them once, in the declaration.
  • Callers cannot skip middle arguments.

Try it yourself

  1. 1
    Write repeat(const std::string& s, int times = 2, char sep = ' ').
  2. 2
    Replace three overloads of print_table with one function using defaults.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

4 questions test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
  • BeginnerWhy is `void draw(int x = 0, int y);` invalid?
  • ModerateWhat is printed?
Practise this topic