Opening the book…
Passing by value and passing by reference is a difficult concept to grasp. Before we begin the discussion, we need to review the concept about variables, memory cells.
It is not just in C++, in all the computer programming language, we use variables in our coding. A variable represents a memory cell. When we declare a variable in a computer program, we assign a memory cell to the variable. When we initialize the variable, we place a data into the memory cell that the variable represents. Once we declare a variable in our program, we can store data, retrieve the data, as well as modify the data in the memory cell through the variable we declared. Each memory cell in the computer also has an address which is a sequence of integers.

In following example, we will write a user-defined function. We will use this function to swap the value of two variables in the main(). In this example, we will go a little bit deeper into a user-defined functions and the variables, memories used in those functions.
Example
Write a user-defined function that can be used to in main() to swap the values of two variables in main().
Following is the first draft of the function and a main() that contains the code for testing the function:

- The user-defined function swap() does not produce any data, so it was declared as void
- x and y are parameters used for receiving values of two variables in main()
- Line 5 declares a double variable t which will served as temperate storage.
- Line 6 copies the value of x into the variable t
- Line 7 assigns the value of y to x
- Line 8 copies the value of t, which is the value of the original value of x, into the variable y
Now, x is holding the original value of y and y is holding the original value of x. The values of the variables x and y are swapped.
In the main():
- line 13 declares two variables a, b and initialize them to 5 and 11.
- line 14 is calling the user-defined function swap() and passes two variables a and b to this function.
- line 15 and 16 display the value of the a and b. If the function swap() is working correctly, then on the output screen, we should see:
The value of a is 11
The value of b is 5
But, from the output of this program:

we can see that the value of a is still 5 and the value of b is still 11. Obviously, the user-defined function swap() is not working correctly.
We will provide a fix first, and then dig out the detail to find out why the original function did not work.

As you can see, the fix is easy: all we did is to put an '&' symbol in front of each parameter x and y of the function.
Now, if we run the program, we can see:

The values of the variable a and b are swapped by the function swap() in main().
Why the original version of swap() was not working?
If we use the original version of the swap() in main(), when it receives two variables a and b at Line 14, the function allocates three memory cells: one represented by t, one represented by x, and one represented by y respectively. The function copies the value of the variable a into x and copies the value of b into y. Then the function use following three statements to swap the values in x and y:
t=x;
x=y;
y=t;
Now, the x is holding the value of b and y is holding the value of a. However, both x and y are local memory cells that belong to the function swap(). What the function swap() did is it swapped the values between those two local memory cells. Those two memory cells are not the same memory cells represented by variable a and b in the main(), thus, the function swap() did not swap the values in a and b, the value of a and b in main() remain unchanged.
In short, in this program, the variables a, b, x, y are representing four memory cells, two of them belong to main() and two of them belong to swap(). The function swap() copies the value of a and b into its local memory x and y and then swap the values between x and y. The function did not swap the value of a and b in main().
Copying the value of the variable the function receives into a local memory cell is referred as "Passing by value", which is fine for most of the functions, but it won't work on the function swap() because the purpose of this function it to swap the values if two variables in main().
By putting an '&' character in front of x and y in the argument of swap(), we change the game.
The second version of swap(), instead of copying the value of a and b into its local memory cells, let x and y represent the memory cells that allocated to the variable a and b in main(). Now, both x and a are representing the same memory cell and so are y and b. Thus, when the swap() uses following statement to swap the values in x and y:
t=x;
x=y;
y=t;
it also swaps the values in a and b in main().
By placing '&' character in front of a parameter of a function, we allow this parameter, which is also a local variable, to directly represent the memory cell represented by the variable the function receives. This is so called "Passing by reference"
Most of user-defined functions use a return statement to send a calculation result to the caller function such as the functions we coded in earlier sections: largest(), BMI(), and sumBetween(), gcf() and so on. But, sometimes, a user-defined function may produce multiple calculation results. It will be easy to just display those data inside of the user-defined function. What if we need to pass them to the caller function?
Example
Write a user-defined function that uses the quadratic formula:

to calculate the solutions of a quadratic equation:

and return the calculation results to the caller function.
Following is a partially completed code of the function:

This function correctly calculated the solutions of a quadratic equation by using the values of the coefficients of the function a, b, and c and the quadratic formula. However, it only returned one of the solutions of the equation to the caller function. We can't use more than one return statement in a function and we can't use a return statement to return more than one result either.
In order to return the second solution of the equation, we revise the code of the function and finish the main() to demonstrate how the caller function receive multiple outputs from a user-defined function:

In Line 4, we add an extra parameter solution2 with '&' in front of it. Now, the variable solution2 in getSolution() and the variable solution_2 in main() represent the same memory cell that is declared for solution_2 in main(). Therefore, the statement in Line 9 assigns the second solution of the equation to variable solution_2 in main().
In main(), Line 17, we call the function getSolution() and receive the first solution of the equation from the return value of the function and receive the second solution through the variable solution_2.
Following is the screenshot of the test run of this program:

This page titled 6.4: Passing by Value vs Passing by Reference was last modified on Wed, 24 Dec 2025 20:37:19 GMT and is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Jin He.
The book could not be loaded just now.