C++ के Default Arguments
In this page:
return_type function_name(type param1, type param2 = default_value) {
// body
}
function_name(argument); // param2 uses the default
Default Arguments का Introduction
एक parameter को इसकी declaration में एक default value दी जा सकती है, जैसे void greet(string name = "Guest"), इसलिए बिना किसी argument के greet() call करना automatically caller से हर बार explicitly इसे supply कराने की बजाय "Guest" इस्तेमाल करता है।
उदाहरण: Introduction to Default Arguments
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
void greet(std::string name = "Guest") {
// Print to the console with cout
std::cout << "Hello, " << name << std::endl;
}
// Program execution starts in main()
int main() {
greet();
greet("Alex");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
कई Default Arguments
एक function में एक साथ कई default parameters हो सकते हैं, और C++ आपके actually दिए values को left से right भरता है — इसलिए अगर आप सिर्फ पहला argument supply करें, इसके बाद का हर parameter अपने default पर fall back होता है।
यह common cases को छोटा रखते हुए ज़रूरत पड़ने पर पूरी customization भी allow करता है।
उदाहरण: Multiple Default Arguments
// Include std::cout and std::cin
#include <iostream>
void configure(int width = 100, int height = 50) {
// Print to the console with cout
std::cout << width << "x" << height << std::endl;
}
// Program execution starts in main()
int main() {
configure();
configure(200);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Function Prototypes में Defaults
जब एक function दोनों declared (prototyped) और अलग से defined हो, default values को सिर्फ declaration में specify करना चाहिए, definition में नहीं दोहराना चाहिए — उन्हें दोनों जगह रखना, या सिर्फ definition में, एक compile error cause करता है।
यह default value का source of truth एक जगह रखता है।
उदाहरण: Defaults in Function Prototypes
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
void greet(std::string name = "Guest");
// Program execution starts in main()
int main() {
greet();
// Return 0 to signal that the program finished successfully
return 0;
}
void greet(std::string name) {
// Print to the console with cout
std::cout << "Hello, " << name << std::endl;
}
Login to try C/C++/Java/PHP code in the editor
Default Arguments के साथ Overloading
Default arguments को function overloading के साथ मिलाना compiler के लिए genuine ambiguity बना सकता है — अगर void show(int a, int b = 5) और void show(int a) दोनों exist करें, show(10) की एक call दोनों से match करती है, और compiler को नहीं पता होगा आपका मतलब कौन सा था।
जब भी आप एक already-overloaded function में default values add करें इस तरह के overlap के लिए check करना worth है।
उदाहरण: Overloading with Default Arguments
#include <iostream>
void show(int a, int b = 5) { std::cout << "a,b: " << a << "," << b << std::endl; }
// void show(int a) {...} // would be ambiguous with show(a, b=5) when called as show(x)
int main() {
show(10);
show(10, 20);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Position Rules
Default arguments parameter list के right-hand end पर group होने चाहिए — एक बार एक parameter की एक default value हो, इसके बाद का हर parameter भी एक होना चाहिए, क्योंकि compiler actually कितने arguments supplied थे उसके आधार पर right से left defaults भरता है।
आप एक defaulted वाले के बाद एक required, non-default parameter नहीं रख सकते।
उदाहरण: Position Rules
#include <iostream>
void order(int a, int b = 2, int c = 3) {
std::cout << a << "," << b << "," << c << std::endl;
}
// void invalid(int a = 1, int b) {} // compile error: default before non-default
int main() {
order(1);
order(1, 5);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक non-default वाले से पहले एक default argument रखना, जैसे
void f(int a = 1, int b), जो एक error है। - declaration और definition दोनों में default value दोहराना, जो एक redefinition error है।
- ऐसे overloads add करना जो defaults से clash करें, ताकि calls ambiguous हो जाएं।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- C++ Functions Introduction
- C++ Function Parameters
- C++ Multiple Function Parameters
- C++ Passing by Reference
- C++ Passing Structures to Functions
- C++ Return Values
- C++ Function Overloading
- C++ Default Arguments
- C++ Recursion
- C++ Inline Functions
- C++ Lambda Functions
- C++ Scope & Lifetime
- C++ Math Functions