100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Default Arguments in C++

Learn how to give function parameters default fallback values in C++ and the rule that default arguments must be trailing parameters.

FunctionsBeginner6 min readJul 7, 2026
Analogies

1. Intro

A default argument lets you assign a fallback value to a function parameter. If the caller omits that argument when calling the function, the default value is used automatically. This is useful when a parameter usually has one common value but occasionally needs to be overridden.

🏏

Cricket analogy: A default argument is like a bowler's standard field setting for a new batsman — used automatically unless the captain explicitly calls for a different, specific field for a known aggressive player like Jos Buttler.

2. Syntax

cpp
returnType functionName(type param1, type param2 = defaultValue) {
    // function body
}

// Example
void greet(string name = "Guest") {
    cout << "Hello, " << name << "!" << endl;
}

3. Explanation

You assign a default value to a parameter using = value in the function's declaration or definition. When the caller provides an argument for that parameter, the supplied value is used instead of the default; when the caller omits it, the default kicks in. The critical rule is that default arguments must be the trailing (rightmost) parameters — once one parameter has a default value, every parameter after it must also have a default value. This is because C++ matches arguments to parameters from left to right, so you cannot skip an earlier parameter while supplying a later one.

🏏

Cricket analogy: You set a default with = value like pre-assigning a standard net bowler for practice unless a specific one is named; the rule that defaults must be trailing mirrors batting order — you can't skip announcing opener 2 while naming opener 1, since positions are filled left to right.

You cannot write void f(int a = 5, int b) — this is a compile-time error because a defaulted parameter (a) is followed by a non-defaulted one (b). Once you default a parameter, every parameter after it must also have a default.

4. Example

cpp
#include <iostream>
using namespace std;

int power(int base, int exponent = 2) {
    int result = 1;
    for (int i = 0; i < exponent; i++) {
        result *= base;
    }
    return result;
}

int main() {
    cout << "power(3): " << power(3) << endl;        // uses default exponent = 2
    cout << "power(2, 5): " << power(2, 5) << endl;   // overrides default
    return 0;
}

5. Output

text
power(3): 9
power(2, 5): 32

6. Key Takeaways

  • A default argument provides a fallback value used when the caller omits that argument.
  • Default values are specified with '= value' in the function signature.
  • Default arguments must be the trailing (rightmost) parameters in the parameter list.
  • A caller-supplied argument always overrides the parameter's default value.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#DefaultArgumentsInC#Default#Arguments#Syntax#Explanation#StudyNotes#SkillVeris#ExamPrep