Pass-by-value vs Pass-by-reference — with C++ examples

Ilyas Karimov
Star Gazers
Published in
3 min readNov 27, 2020

--

Hello, Folks! I’ve been thinking for days about how to explain this topic so that newcomers understand it better and don’t need to read further. I think I found that way, but after reading the article, how about you tell me if I am right.

P.S. My code will be thoroughly commented.

pass-by-reference vs pass-by-value

When calling a function, the arguments in the function can be either passed-by-value or passed-by-reference. There are also callee and caller terms used as a function called by another and a function that calls another function(a callee) respectively. Additionally, the value that is passed in the function call is called the actual parameter, and the value received by the function from the call is called the formal parameter.

Pass-by-value

When the variable is passed-by-value, the copy of the actual parameter’s value is stored in the memory, and the callee and caller have two independent variables with the same value, but the caller cannot see the modification by the caller. To put it simply, if we want to access and modify a variable within a function, the original value will remain untouched, i.e. the value will not change. What we use is usually pass-by-value.

Pass by reference

In this case, the memory address of that variable is passed directly to the function. Callee and Caller run the same variable, so if the callee makes any modifications, it is also seen by the caller, that is, it affects its variable. In many programming languages, passing by reference can be achieved by putting & symbol in front of a variable.

Now let’s write a simple exponentiation code. You can understand it by paying attention to the following code and comments. So, the program will print the answer after multiplying that number by itself. I write this as an example of pass-by-value.

#include <iostream>int expo(int x){//callee - the function itself, x- formal parameterreturn x= x*x;// return should be int type, returning x*x.}int main() {int n; //actual parameterscanf("%d", &n); //scanning from consoleprintf("%d %d\n", expo(n), n); 
/* expo(n) is caller here. printing expo(n) and also actual parameter, to show you actually n is not affected. the answers will be still 25 and 5. the variable n is not affected. */
}

For example, if you enter 5 during a scanf (when you enter a number from the console), the answers will be 25 (the answer returned from expo(n)) and 5, ie the value of the variable n. The answer will look like this: 25 5. Remember to look carefully at the printf function to understand it better. That’s why this is called passing-by-value, check that coffee gif again at the start.

Now let’s look at the pass-by-reference code.

#include <iostream>int expo(int &x){ //callee - the function, referenced by &. return x = x*x; // return should be int type, returning x*x.}int main() {int n; //actual parameterscanf("%d", &n); //scanning from consoleprintf("%d %d\n", expo(n), n);/* expo(n) is caller. printing expo(n) result and also actual parameter, to show you actually n is not affected. */}

Here, if you enter 5 during the scan, you will see 25 25 answers. The first 25 is the response returned from the expo (n) function, and the next is the value of the variable n. If you don’t believe the answer, you can copy and paste the code yourself into a C++ programming compiler.

We have come to the end of today’s article. Don’t forget to ask questions about the unclear part(s). Don’t forget to read my other articles. Peace ✌🏼

References:

  1. N, T. (2018, March 08). Passing by Value vs. by Reference Visual Explanation. Retrieved from https://blog.penjee.com/passing-by-value-vs-by-reference-java-graphical/#:~:text=The terms “pass by value,where the value is stored.
  2. Pass by value vs. pass by reference. (n.d.). Retrieved from https://www.educative.io/edpresso/pass-by-value-vs-pass-by-reference
  3. Sebesta, R. W. (2019). Examples of Multiple Selectors. In Concepts of programming languages. NY, NY: Pearson.

--

--

Ilyas Karimov
Star Gazers

Master of Computer Science and Data Analytics at ADA/GW Universities, Researcher, Psychology-lover, Meme-maker, Musician, Writer, AI & Sarcasms!