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

C++ में Reference से Passing

एक function parameter को & से declare करना इसे caller के argument का एक reference बनाता है, function को एक copy पर काम करने की बजाय सीधे original variable modify करने देते हुए।
Syntax
cpp
return_type function_name(type &parameter) {
  parameter = new_value;  // changes the caller's variable
}

Value से Pass करना बनाम Reference से Pass करना

Default से, C++ arguments को value से pass करता है, function को अपनी copy देते हुए, लेकिन एक parameter के type में & add करना इसे एक reference बनाता है, function को इसकी बजाय caller के original variable पर operate करने देते हुए।

उदाहरण: Pass by Value vs Pass by Reference

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

void byValue(int x) { x = 99; }
void byReference(int &x) { x = 99; }

// Program execution starts in main()
int main() {
	int a = 1, b = 1;
	byValue(a);
	byReference(b);
	// Print to the console with cout
	std::cout << "a=" << a << " b=" << b << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Caller का Variable Modify करना

एक reference parameter caller के argument के साथ same memory share करता है, इसलिए function के अंदर parameter को किया गया कोई भी assignment तुरंत caller को दिखने वाली value बदल देता है।

उदाहरण: Modifying the Caller's Variable

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

void reset(int &score) {
	score = 0;
}

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

Reference Parameters के जरिए कई Values Return करना

क्योंकि एक function सीधे सिर्फ एक value return कर सकता है, reference parameters एक function के लिए अपने caller को effectively कई results वापस देने की एक common technique हैं।

उदाहरण: Returning Multiple Values via Reference Parameters

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

void minMax(int a, int b, int &minVal, int &maxVal) {
	if (a < b) { minVal = a; maxVal = b; }
	else { minVal = b; maxVal = a; }
}

// Program execution starts in main()
int main() {
	int lo, hi;
	minMax(8, 3, lo, hi);
	// Print to the console with cout
	std::cout << "min=" << lo << " max=" << hi << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Performance के लिए बड़े Objects Reference से Pass करना

एक बड़े string या struct जैसे एक बड़े object को reference से pass करना इसे पूरी तरह copy करने की cost avoid करता है, क्योंकि function को एक duplicate की बजाय existing data का एक reference मिलता है।

उदाहरण: Passing Large Objects by Reference for Performance

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

void printLength(const std::string &text) {
	// Print to the console with cout
	std::cout << "Length: " << text.length() << std::endl;
}

// Program execution starts in main()
int main() {
	std::string bigText = "A long string that would be costly to copy";
	printLength(bigText);
	// Return 0 to signal that the program finished successfully
	return 0;
}

const Reference Parameters

const को एक reference parameter के साथ combine करना, const Type& लिखा गया, argument को इसे copy किए बिना efficiently pass करता है साथ ही function को accidentally caller के original data modify करने से भी रोकते हुए।

उदाहरण: const Reference Parameters

cpp
#include <iostream>
#include <string>

void show(const std::string &name) {
	std::cout << "Hello, " << name << std::endl;
	// name = "Changed"; // compile error: cannot modify a const reference
}

int main() {
	show("Alex");
	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. parameter में & भूल जाना और original की बजाय एक copy modify करना।
  2. एक non-const reference parameter को 5 जैसा एक literal pass करना, जो compile नहीं होता।
  3. एक local variable का reference return करना, जो function खत्म होने के बाद exist नहीं करता।

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.