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
#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;
}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:
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):
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.
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++?
How do default arguments work in C++?
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++?
Should default arguments go in the declaration or the definition?
What is the difference between default arguments and function overloading?
Default arguments क्या हैं?
Default arguments आपको किसी parameter को पहले से एक value देने देते हैं, ताकि caller उसे छोड़ सके। अगर वे इसे छोड़ दें, default इस्तेमाल होता है; अगर value भेजें, वह जीतती है। यह कुछ parameters को प्रभावी रूप से वैकल्पिक बनाता है।
एक चलता उदाहरण
#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;
}Hello, Amit!
Welcome, Riya!
Trailing नियम
Defaults सबसे दाईं parameters पर होनी चाहिए। एक बार किसी parameter की default हो, उसके दाईं ओर के सभी parameters की भी होनी चाहिए:
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 में नहीं):
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 का ध्यान रखें।