C++ में Reference से Passing
In this page:
return_type function_name(type ¶meter) {
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Caller का Variable Modify करना
एक reference parameter caller के argument के साथ same memory share करता है, इसलिए function के अंदर parameter को किया गया कोई भी assignment तुरंत caller को दिखने वाली value बदल देता है।
उदाहरण: Modifying the Caller's Variable
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Reference Parameters के जरिए कई Values Return करना
क्योंकि एक function सीधे सिर्फ एक value return कर सकता है, reference parameters एक function के लिए अपने caller को effectively कई results वापस देने की एक common technique हैं।
उदाहरण: Returning Multiple Values via Reference Parameters
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Performance के लिए बड़े Objects Reference से Pass करना
एक बड़े string या struct जैसे एक बड़े object को reference से pass करना इसे पूरी तरह copy करने की cost avoid करता है, क्योंकि function को एक duplicate की बजाय existing data का एक reference मिलता है।
उदाहरण: Passing Large Objects by Reference for Performance
// 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;
}
Login to try C/C++/Java/PHP code in the editor
const Reference Parameters
const को एक reference parameter के साथ combine करना, const Type& लिखा गया, argument को इसे copy किए बिना efficiently pass करता है साथ ही function को accidentally caller के original data modify करने से भी रोकते हुए।
उदाहरण: const Reference Parameters
#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;
}
Login to try C/C++/Java/PHP code in the editor
- parameter में
&भूल जाना और original की बजाय एक copy modify करना। - एक non-const reference parameter को
5जैसा एक literal pass करना, जो compile नहीं होता। - एक local variable का reference return करना, जो function खत्म होने के बाद exist नहीं करता।
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