C++ Passing by Reference
In this page:
Pass by Value vs Pass by Reference
By default, C++ passes arguments by value, giving the function its own copy, but adding & to a parameter's type makes it a reference, letting the function operate on the caller's original variable instead.
Example: Pass by Value vs Pass by Reference
#include <iostream>
void byValue(int x) { x = 99; }
void byReference(int &x) { x = 99; }
int main() {
int a = 1, b = 1;
byValue(a);
byReference(b);
std::cout << "a=" << a << " b=" << b << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Modifying the Caller's Variable
A reference parameter shares the same memory as the caller's argument, so any assignment made to the parameter inside the function immediately changes the value the caller sees.
Example: Modifying the Caller's Variable
#include <iostream>
void reset(int &score) {
score = 0;
}
int main() {
int score = 50;
reset(score);
std::cout << score << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Returning Multiple Values via Reference Parameters
Since a function can only return one value directly, reference parameters are a common technique for a function to effectively hand back several results to its caller.
Example: Returning Multiple Values via Reference Parameters
#include <iostream>
void minMax(int a, int b, int &minVal, int &maxVal) {
if (a < b) { minVal = a; maxVal = b; }
else { minVal = b; maxVal = a; }
}
int main() {
int lo, hi;
minMax(8, 3, lo, hi);
std::cout << "min=" << lo << " max=" << hi << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Passing Large Objects by Reference for Performance
Passing a large object like a big string or a struct by reference avoids the cost of copying it entirely, since the function receives a reference to the existing data instead of a duplicate.
Example: Passing Large Objects by Reference for Performance
#include <iostream>
#include <string>
void printLength(const std::string &text) {
std::cout << "Length: " << text.length() << std::endl;
}
int main() {
std::string bigText = "A long string that would be costly to copy";
printLength(bigText);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
const Reference Parameters
Combining const with a reference parameter, written as const Type&, passes the argument efficiently without copying it while also preventing the function from accidentally modifying the caller's original data.
Example: 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
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