FAQ: References and Pointers - Pass-By-Reference with Const

This community-built FAQ covers the “Pass-By-Reference with Const” 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 with Const

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 Language Help.

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

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

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 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!

Is there any difference between “const int &a” and “int const &a” ?
C++ doesn’t seem to report any problems about this.

https://www.stroustrup.com/bs_faq2.html#constplacement

ANSWERED IN EDIT

I’m getting different results from the lesson before this compared to this one doing the same thing. Why is the variable not changing? See example below. Is it the way Codecademy has the VM or whatever set up or am I missing something?

#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"; std::cout << num << "\n"; }

Outputs:

3
9
9

#include <iostream> int square(int &i) { return i * i; } int main() { int side = 5; std::cout << square(side) << "\n"; std::cout << square(side) << "\n"; std::cout << side << "\n"; }

Outputs:

25
25
5

How come it’s not this output like the less before?

Expected:

25
625
625

EDIT: I figured it out. It’s because in the first lesson you change the value of i (i = i * 3, then return) and in the second lesson you return i without modifying it.

I guess I’m a bit confused. I generally get what’s going on, but I don’t understand why you’d choose to pass-by-reference with a const &variable instead of just choosing to pass-by-value instead. It seems like they achieve the same thing. Any benefit to choosing one over the other?