← Back to C++ Course | Chapter 5: Functions | Lesson 8 of 13

C++ के Default Arguments

एक default argument एक backup value है जिसे एक function इस्तेमाल करता है अगर आप इसे skip करें, cheese के साथ आने वाली एक pizza की तरह जब तक आप अन्यथा न कहें। यह कुछ inputs को optional बनाता है।
Syntax
cpp
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

cpp
// 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;
}

कई Default Arguments

एक function में एक साथ कई default parameters हो सकते हैं, और C++ आपके actually दिए values को left से right भरता है — इसलिए अगर आप सिर्फ पहला argument supply करें, इसके बाद का हर parameter अपने default पर fall back होता है।

यह common cases को छोटा रखते हुए ज़रूरत पड़ने पर पूरी customization भी allow करता है।

उदाहरण: Multiple Default Arguments

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
#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;
}

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

cpp
#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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक non-default वाले से पहले एक default argument रखना, जैसे void f(int a = 1, int b), जो एक error है।
  2. declaration और definition दोनों में default value दोहराना, जो एक redefinition error है।
  3. ऐसे overloads add करना जो defaults से clash करें, ताकि calls ambiguous हो जाएं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.