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

C++ Function के Parameters

Parameters एक function में खाली slots हैं जिन्हें आप हर बार भरते हैं, एक recipe में blanks की तरह। आप एक value की copy या खुद original pass कर सकते हैं।
Syntax
cpp
return_type function_name(type param) {         // pass by value
  // body
}

return_type function_name(type &param) {        // pass by reference
  // body
}

Value से Pass करना

Value से pass करना function को आपके दिए किसी भी argument की अपनी independent copy देता है, इसलिए function के अंदर parameter में किए गए कोई भी changes caller में original variable पर zero असर डालते हैं।

यह सबसे safe default है, क्योंकि caller का data unintended modification से पूरी तरह protected है।

उदाहरण: Pass by Value

cpp
// Include std::cout and std::cin
#include <iostream>

void increment(int x) {
	x = x + 1;
}

// Program execution starts in main()
int main() {
	// Declare num and set it to 5
	int num = 5;
	increment(num);
	// Print to the console with cout
	std::cout << num << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Reference से Pass करना

void update(int &x) जैसे parameter type में & इस्तेमाल करके reference से pass करना function को एक copy की बजाय खुद original variable तक direct access देता है — function के अंदर किया कोई भी change caller को भी तुरंत visible होता है।

यह essential है जब भी एक function को actually caller का data modify करना हो, जैसे दो values swap करना।

उदाहरण: Pass by Reference

cpp
// Include std::cout and std::cin
#include <iostream>

void update(int &x) {
	x = x + 1;
}

// Program execution starts in main()
int main() {
	// Declare num and set it to 5
	int num = 5;
	update(num);
	// Print to the console with cout
	std::cout << num << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Const Parameters

const को एक reference parameter के साथ combine करना, जैसे void print(const string &name), आपको reference passing का performance benefit देता है (कोई copy नहीं बनती, जो strings जैसे बड़े objects के लिए मायने रखता है) साथ ही guarantee करते हुए कि function accidentally caller का data modify नहीं कर सकता।

यह pattern exactly इसी कारण real C++ code में बहुत common है।

उदाहरण: Const Parameters

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

void print(const std::string &name) {
	// Print to the console with cout
	std::cout << "Hello, " << name << std::endl;
}

// Program execution starts in main()
int main() {
	print("Alex");
	// Return 0 to signal that the program finished successfully
	return 0;
}

कई Parameters

एक function को ज़रूरत के हिसाब से कितने भी parameters accept कर सकता है, अपनी definition और हर call site में order में और commas से अलग list किए गए — जो arguments आप pass करते हैं उन्हें parameter list के order और count से match करना चाहिए (या expected types में convertible होना चाहिए)।

उदाहरण: Multiple Parameters

cpp
// Include std::cout and std::cin
#include <iostream>

int addThree(int a, int b, int c) {
	// Send a + b + c back to the caller
	return a + b + c;
}

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << addThree(1, 2, 3) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Array Parameters

Arrays default से C++ में reference से pass होते हैं, एक explicit & के बिना भी, जिसका मतलब है function द्वारा array elements में किए गए कोई भी changes caller में original array को affect करते हैं।

यह ordinary variables से अलग है और array parameters को value parameters जैसे behave करने की उम्मीद करने वाले beginners के लिए एक common surprise है।

उदाहरण: Array Parameters

cpp
// Include std::cout and std::cin
#include <iostream>

void doubleValues(int arr[], int size) {
	// Loop: repeat while the condition holds
	for (int i = 0; i < size; i++) {
		arr[i] *= 2;
	}
}

// Program execution starts in main()
int main() {
	int nums[] = {1, 2, 3};
	doubleValues(nums, 3);
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		// Print to the console with cout
		std::cout << nums[i] << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	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. यह उम्मीद करना कि एक function caller का variable बदल देगा जब इसे value से एक copy मिली थी।
  2. एक array pass करना और parameter पर sizeof इस्तेमाल करना, जो array की बजाय एक pointer का size देता है।
  3. एक reference parameter पर const भूल जाना, ताकि function accidentally argument बदल सके।

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.