Passing by Value and by Reference
In this lesson, you will learn how a function can change a variable of the code that called it, and how to pass a big vector to a function without copying all of it. References do both. In the second part of the lesson, you will meet pointers: they work with the addresses of variables in memory, and you will see them in many C++ programs and libraries.
First, recall what happens when you call a function. The value of the argument is copied into the parameter. The parameter is a new local variable, so when the function changes it, it changes only its own copy:
#include <iostream>
void addPoint(int points) {
points = points + 1;
std::cout << "In the function: " << points << std::endl;
}
int main() {
int score = 10;
addPoint(score);
std::cout << "In main: " << score << std::endl;
return 0;
}This prints:
In the function: 11
In main: 10This is called passing by value: the function got only the value 10, not the variable score. It was the same with the vector in the Functions lesson: the function got a copy of it. Most of the time that's good, because a function can't change your variables by accident.
But when you want the function to change the variable, write the & sign after the type of the parameter:
#include <iostream>
void addPoint(int& points) {
points = points + 1;
}
int main() {
int score = 10;
addPoint(score);
addPoint(score);
std::cout << "Score: " << score << std::endl;
return 0;
}This prints:
Score: 12The parameter int& points is a reference to an int. A reference isn't a copy: it's another name for the variable you pass to the function. In the call addPoint(score), points inside the function is just another name for score, so whatever the function does to points happens to score. This is called passing by reference.
The call looks the same as before. You can only tell that the function may change your variable from the & in the function's first line. Spaces around & don't matter: int& points and int &points mean the same. This course writes & right after the type.
Watch out for these mistakes:
- When you forget the
&, the program still compiles and runs, but the variable doesn't change, and no error is shown. When a function "does nothing", check the&first. - A reference parameter needs a variable. The call
addPoint(5)ends with the errorno matching function for call to 'addPoint'(no function matches the call) and the notecandidate function not viable: expects an lvalue for 1st argument. An lvalue is roughly "something you can assign to", such as a variable. The number5isn't a variable that the function could change.
Instructions
- 1
The program prints
Money: 50, because the functiondoubleItdoubles only its own copy. Change its parameter to a reference, so that the function doubles the variablemoney. The program then printsMoney: 100. - 2
Below the function
doubleIt, write a functionspendwith two parameters: a referencemoneyof typeint&and an amountamountof typeint. The function subtracts the amount from the money. At the end ofmain(beforereturn 0;), callspend(money, 30)and below it print the money the same way as above. The program printsMoney: 100and thenMoney: 70.