References and Functions II (Pass by Reference)

In general, functions are used in programming to perform specific tasks and only return one value (if necessary) after processing any input arguments. It’s important to note that the memory allocated to a function will be released once the function has completed its task and returned any necessary value.

But what if we need more than one value from a particular function? Before discussing this, let’s first become familiar with the concept of reference variables.

Reference variable

To understand reference variables in C++, it’s important to understand the behavior of the ampersand (&) operator first. We can use this & operator to obtain the memory address of a variable.

Let’s look at an example.

Get hands-on with 1200+ tech skills courses.