🔵 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.

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 का ध्यान रखें।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में default arguments क्या हैं?
Default arguments वे values हैं जो declaration में function parameters को दी जाती हैं, ताकि callers उन arguments को छोड़ सकें। अगर caller उन्हें छोड़ दे, defaults इस्तेमाल होते हैं; अगर values दें, वे defaults को override करती हैं। यह कुछ parameters को प्रभावी रूप से वैकल्पिक बनाता है।
C++ में default arguments कैसे काम करते हैं?
आप function header में parameter को value देते हैं, जैसे void greet(string msg = "Hi")greet() call करना "Hi" इस्तेमाल करता है, जबकि greet("Hello") आपकी value इस्तेमाल करता है। Compiler किसी भी छूटे trailing arguments को उनके defaults से भर देता है।
क्या C++ में किसी भी parameter की default value हो सकती है?
Defaults सबसे दाईं (trailing) parameters को दी जानी चाहिए। एक बार किसी parameter की default हो, उसके बाद हर parameter की भी होनी चाहिए। ऐसा इसलिए कि arguments बाएँ से दाएँ मिलाए जाते हैं, तो आप बीच वाला नहीं छोड़ सकते।
Default arguments declaration में हों या definition में?
अगर prototype हो तो default function declaration में रखें, और definition में न दोहराएँ। दोनों जगह वही default देना error है, तो जब function परिभाषित होने से पहले declare हो तब declaration चुनें।
Default arguments और function overloading में क्या अंतर है?
Default arguments एक function को कम arguments वाले calls को defaults भरकर संभालने देते हैं, जबकि overloading अलग argument सूचियों के लिए अलग functions देता है। Defaults वैकल्पिक values के लिए सरल हैं; overloading तब चाहिए जब व्यवहार या types सच में भिन्न हों।
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.