Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🔵 Core C++  ·  Lesson 31

Default Arguments in C++

In this lesson: Give function parameters default values in C++ — rules, order and examples with output. In Hindi & English.

What are default arguments?

Default arguments let you give a parameter a value in advance, so the caller can leave it out. If they omit it, the default is used; if they pass a value, that wins. This makes some parameters effectively optional.

A working example

C++
#include <iostream>
using namespace std;

void greet(string name, string msg = "Hello") {
    cout << msg << ", " << name << "!\n";
}

int main() {
    greet("Amit");             // uses default msg
    greet("Riya", "Welcome");  // overrides default
    return 0;
}
Output:
Hello, Amit!
Welcome, Riya!

The trailing rule

Defaults must be on the rightmost parameters. Once one parameter has a default, all parameters to its right must have defaults too:

C++
void f(int a, int b = 2, int c = 3);   // OK
// void g(int a = 1, int b);           // ERROR: b has no default

This is because arguments fill in left to right — you cannot skip a middle one and supply a later one.

Where to put the default

If your function has a separate prototype, put the default there (not in the definition):

C++
int power(int base, int exp = 2);   // default in prototype

int power(int base, int exp) {      // no default here
    int r = 1;
    for (int i = 0; i < exp; i++) r *= base;
    return r;
}

Putting the same default in both places is a compile error.

With overloading

Default arguments can overlap with overloading, but be careful — if a defaulted call could match two functions, the compiler reports an ambiguity. Prefer one approach or the other for a given set of calls.

Common mistakes

  • Putting a default on a middle parameter but not the ones after it.
  • Repeating the default in both the declaration and the definition.
  • Creating an ambiguity between a defaulted function and an overload.
  • Assuming you can skip an argument in the middle of a call — you cannot.
🏋️ Practice

Write a function printLine(char symbol = '-', int length = 20) that prints a line of the given character. Call it with no arguments, one argument, and both, and observe the output.

Summary

  • Default arguments give parameters fallback values, making them optional.
  • A supplied value overrides the default; an omitted one uses it.
  • Defaults must be on the trailing (rightmost) parameters.
  • Put the default in the prototype, not repeated in the definition.
  • Watch for ambiguity when mixing defaults with overloading.

Frequently Asked Questions

What are default arguments in C++?
Default arguments are values given to function parameters in the declaration, so that callers can leave those arguments out. If the caller omits them, the defaults are used; if they supply values, those override the defaults. It makes some parameters effectively optional.
How do default arguments work in C++?
You assign a value to a parameter in the function header, such as void greet(string msg = "Hi"). Calling greet() uses "Hi", while greet("Hello") uses your value. The compiler fills in any missing trailing arguments with their defaults.
Can any parameter have a default value in C++?
Defaults must be given to the rightmost (trailing) parameters. Once a parameter has a default, every parameter after it must also have one. This is because arguments are matched left to right, so you cannot skip a middle one.
Should default arguments go in the declaration or the definition?
Put the default in the function declaration (the prototype) if you have one, and not repeat it in the definition. Specifying the same default in both places is an error, so choose the declaration when the function is declared before it is defined.
What is the difference between default arguments and function overloading?
Default arguments let one function handle calls with fewer arguments by filling in defaults, while overloading provides separate functions for different argument lists. Defaults are simpler for optional values; overloading is needed when the behaviour or types genuinely differ.
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicInline Functions Your progress: 31 / 81 (38%) · Lesson 32 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free