🔵 Core C++  ·  Lesson 31

Default Arguments

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.

Default arguments क्या हैं?

Default arguments आपको किसी parameter को पहले से एक value देने देते हैं, ताकि caller उसे छोड़ सके। अगर वे इसे छोड़ दें, default इस्तेमाल होता है; अगर value भेजें, वह जीतती है। यह कुछ parameters को प्रभावी रूप से वैकल्पिक बनाता है।

एक चलता उदाहरण

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

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

int main() {
    greet("Amit");             // default msg use hota hai
    greet("Riya", "Welcome");  // default override
    return 0;
}
Output:
Hello, Amit!
Welcome, Riya!

Trailing नियम

Defaults सबसे दाईं parameters पर होनी चाहिए। एक बार किसी parameter की default हो, उसके दाईं ओर के सभी parameters की भी होनी चाहिए:

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

ऐसा इसलिए कि arguments बाएँ से दाएँ भरते हैं — आप बीच वाला छोड़कर बाद वाला नहीं दे सकते।

Default कहाँ रखें

अगर आपके function का अलग prototype हो, default वहाँ रखें (definition में नहीं):

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

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

दोनों जगह वही default रखना compile error है।

Overloading के साथ

Default arguments overloading से overlap कर सकते हैं, पर सावधान रहें — अगर कोई defaulted call दो functions से मेल खा सके, compiler ambiguity बताता है। किसी दिए calls के समूह के लिए एक या दूसरा तरीका पसंद करें।

आम गलतियाँ

  • बीच वाले parameter पर default रखना पर उसके बाद वालों पर नहीं।
  • Declaration और definition दोनों में default दोहराना।
  • Defaulted function और overload के बीच ambiguity बनाना।
  • यह मानना कि आप call के बीच में argument छोड़ सकते हैं — नहीं छोड़ सकते।
🏋️ अभ्यास

एक function printLine(char symbol = '-', int length = 20) लिखें जो दिए character की एक line print करे। इसे बिना arguments, एक argument, और दोनों के साथ call करें, और output देखें।

सारांश

  • Default arguments parameters को fallback values देते हैं, उन्हें वैकल्पिक बनाते हुए।
  • दी गई value default को override करती है; छोड़ी गई उसे इस्तेमाल करती है।
  • Defaults trailing (सबसे दाईं) parameters पर होनी चाहिए।
  • Default prototype में रखें, definition में न दोहराएँ।
  • Defaults को overloading से मिलाते समय ambiguity का ध्यान रखें।
← 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.