FAQ: References and Pointers - Pass-By-Reference

This community-built FAQ covers the “Pass-By-Reference” exercise from the lesson “References and Pointers”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C++

FAQs on the exercise Pass-By-Reference

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Difference between int &i and int& i ?

I see some code examples in Github use int&

1 Like

Forget to Google

Found an answer on Stackoverflow,

#include <iostream>
int triple(int &i) {
  i = i * 3;
  return i;
}

int main() {
  int num = 1;
  std::cout << triple(num) << "\n";
  std::cout << triple(num) << "\n";
}

this would print 3 and then 9… why is that???
isn’t a reference simply a “copy” of the variable???
so since num = 1; wouldn’t that mean that when the function is called its basically just inputting 3 times 1?? why does it take the answer of 3 and multiply it again?

1 Like

I’m not exactly sure either, but best guess is this:
When we turn int i into the pass-by reference int &i
any variables used as arguments in the triple function are modified when i is returned. Thus, when we triple(num) and return i = 3 the first time, we’ve also modified the value of num such that num = 3. Running the same output command with our modified value produces the predictable result.

No, passing by value passes a copy of the value; passing by reference passes the “address” of the value’s storage location.

Imagine computer memory as a long line of lockers in a high school hallway. Locker #210 (maybe named “JanesLocker”) has a post-it note inside with the message “I love you, Jane” written on it.

Then you realize you need in locker #300 (maybe named “MarysLocker”) the same value as what’s in #210. You have two choices:

  1. Pass by Value - you can write a copy of the post-it note that also says “I love you, Jane” and put that copy into #300, or

  2. Pass by Reference - you can write a post-it note for #300 that says, “See #210”.

Later, you realize you need to change the message in #300 to, “I love you, Mary”. If you go to locker #300 (“MarysLocker”) , and see the passed-by-value note, you can change that note, and it will not affect the note in #210 (“JanesLocker”). But if you see the passed-by-reference note that has the message to “See #210”, you’ll then go to #210, and change that post-it note, essentially changing the message for both lockers.

It’s not an exact analogy, but maybe it’ll help you grasp the concept.

My above analogy probably fits better with the C language and pointers, rather than C++ and references. Perhaps a better analogy for C++/references is the same set of high school lockers, but with only one locker in play, #210, named both “JanesLocker” and “MarysLocker”. They’re the same locker, holding the same data, just by different names. This is like pass-by-reference. Modify the data in “MarysLocker” and it automagically modifies the data in "JanesLocker’, and vice-versa.

Pass-by-value would still be like option 1 in the above analogy: two separate lockers, with one getting a copy of the data. Modify either copy, it has no effect on the other copy.

After you get through the lesson on pointers, you can run this code to see the difference in pass-by-value and pass-by-reference:

#include <iostream>

void xperiment(int &p) {
  std::cout << "\n\nThe value of 'p' = " << p;
  std::cout << "\nThe address of 'p' = " << &p;
}

int main() {
  
  int power = 9000;
  
  // Create pointer
  int* ptr = &power;
  
  // Print ptr
  std::cout << "The address of 'power' = " << ptr << "\n";
  
  // Print *ptr
  std::cout << "The value of 'power' = " << *ptr;

  xperiment(power);

}

When run, you can see that the address of both “power” and “p” are the same (one locker, with two names).

But change the line

void xperiment(int &p)

to

void xperiment(int p)

removing the “&”, you’ll see that “power” and “p” have different addresses (two different lockers, with “p” getting a copy of the data (“9000”) that’s in “power”.