Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

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: 10

This 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: 12

The 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 error no matching function for call to 'addPoint' (no function matches the call) and the note candidate function not viable: expects an lvalue for 1st argument. An lvalue is roughly "something you can assign to", such as a variable. The number 5 isn't a variable that the function could change.

Instructions

  1. 1

    The program prints Money: 50, because the function doubleIt doubles only its own copy. Change its parameter to a reference, so that the function doubles the variable money. The program then prints Money: 100.

  2. 2

    Below the function doubleIt, write a function spend with two parameters: a reference money of type int& and an amount amount of type int. The function subtracts the amount from the money. At the end of main (before return 0;), call spend(money, 30) and below it print the money the same way as above. The program prints Money: 100 and then Money: 70.

Back

1/10

  1. 1. Passing by Value and by Reference
  2. 2. Swapping Two Variables
  3. 3. Changing a Vector in a Function
  4. 4. Const References
  5. 5. Addresses and Pointers
  6. 6. The & and * Signs
  7. 7. Pointers as Parameters
  8. 8. The Null Pointer
  9. 9. Reference or Pointer?
  10. 10. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.