Loading...
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
Passing arguments by reference allows functions to modify the original variable and avoid the overhead of copying large data objects.
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
int x = 5, y = 10;
swap(x, y); // x=10, y=5The function uses the original variable's memory. This is O(1) time and space.
The function can "return" multiple values by modifying its reference parameters.
Use const T& to get the performance benefits of passing by reference without allowing the function to modify the data.
void printBig(const string &text);