C++ Default Arguments

C++ default arguments are preset values for function parameters. If a function call leaves out an argument, C++ uses the default value written in the function declaration.

Default arguments are helpful when most calls use the same value, but you still want callers to override it when needed.

Basic Example

Write a default argument by adding = and a value after a parameter in the function header.

#include <iostream>

void printTotal(int price, int quantity = 1) {
    std::cout << "Total: " << price * quantity << std::endl;
}

int main(void) {
    printTotal(12);
    printTotal(12, 4);

    return 0;
}

Output:

Total: 12
Total: 48

The first call, printTotal(12), supplies only price. Since quantity was not provided, C++ uses its default value of 1. The second call supplies 4, so the default is ignored.

Multiple Default Arguments

A function can have more than one default argument. When you leave out arguments, C++ fills them from left to right based on the parameters that remain.

#include <iostream>
#include <string>

void createProfile(std::string name, int level = 1, bool active = true) {
    std::cout << name << " level " << level;

    if (active) {
        std::cout << " active";
    } else {
        std::cout << " inactive";
    }

    std::cout << std::endl;
}

int main(void) {
    createProfile("Ava");
    createProfile("Ben", 3);
    createProfile("Cara", 5, false);

    return 0;
}

Output:

Ava level 1 active
Ben level 3 active
Cara level 5 inactive

The call createProfile("Ava") uses both defaults. The call createProfile("Ben", 3) overrides level but still uses the default active value. The last call supplies all arguments.

Defaults Must Come Last

Once a parameter has a default value, every parameter after it must also have a default value. This rule keeps function calls clear.

For example, void show(int size = 10, int count) is not valid because a caller could write show(5), and C++ would not know whether 5 means size or count. Put required parameters first and optional parameters after them.

Function Declarations

When a function has a separate declaration and definition, put the default argument in the declaration. Do not repeat the same default value in the later definition.

#include <iostream>

void repeatNumber(int number, int times = 2);

void repeatNumber(int number, int times) {
    for (int i = 0; i < times; i++) {
        std::cout << number << std::endl;
    }
}

int main(void) {
    repeatNumber(7);
    repeatNumber(4, 3);

    return 0;
}

Output:

7
7
4
4
4

The declaration tells the compiler that times can be omitted. The definition describes what the function does, using the value that was passed or supplied by the default.

When To Use Default Arguments

  • Use a default when one value is common for most calls.
  • Keep default values simple and easy to understand.
  • Avoid too many defaults in one function, because calls can become hard to read.
  • If different combinations mean different jobs, function overloading may be clearer.

Takeaway: default arguments make function calls shorter when optional values have sensible defaults.