Learn

So what’s a good use case for references? Let’s take a look.

Previously, when we passed parameters to a function, we used normal variables and that’s known as pass-by-value. But because the variables passed into the function are out of scope, we can’t actually modify the value of the arguments.

Pass-by-reference refers to passing parameters to a function by using references. When called, the function can modify the value of the arguments by using the reference passed in.

This allows us to:

  • Modify the value of the function arguments.
  • Avoid making copies of a variable/object for performance reasons.

The following code shows an example of pass-by-reference. The reference parameters are initialized with the actual arguments when the function is called:

void swap_num(int &i, int &j) { int temp = i; i = j; j = temp; } int main() { int a = 100; int b = 200; swap_num(a, b); std::cout << "A is " << a << "\n"; std::cout << "B is " << b << "\n"; }

Notice that the int &i and int &j are the parameters of the function swap_num().

When swap_num() is called, the values of the variables a and b will be modified because they are passed by reference. The output will be:

A is 200 B is 100

Suppose we didn’t pass-by-reference here and have the parameters as simply int i and int j in the swap_num() function, then i and j would swap, but a and b wouldn’t be modified.

And the output will then be:

A is 100 B is 200

To reiterate, using references as parameters allows us to modify the arguments’ values. This can be very useful in a lot cases.

Instructions

1.

Take a look at the program in the code editor. There is a function called triple() and there is a main() where we call triple() twice.

What do you think this code will output?

Run the program to see if you are correct.

2.

Change the parameter from int i to int &i.

We are doing pass-by-reference instead of pass-by-value. What do you think this will output now?

Run the code again.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?