Pass by value Vs. Pass by reference

Flare
3 min readMar 3, 2022

--

Functions are one of the great things to achieve clean and readable code, but when it comes to data manipulation the developer or the programmer must be knowledgeable about the behavior of the used programming language.

On this matter, when dealing with data there are two things we should know about: variables scope (lifetime of variables in the program), and pass by value versus pass by reference/address. Taking C programming language for instance, when we declare a variable its scope is defined in the program based on where we declared it.

1. Pass By Value

So let’s take the following example:

By running the program above, we will have an output of:

  • Initial value of a is: 42
  • Value of a inside passByValue function: 126
  • Value of a is: 42

The initial value of a is 42 since that’s the number we assigned to the variable in the main function, then we call passByValue function and we get a print statement showing the value of a inside of the function which is 126, and then we print a after calling the function but the value is still 42!

That’s called pass by value as we take the value or as we can say a copy of the variable and change it inside of the local scope (educative, n.d), which is in our case passByValue function; where the value of a was multiplied by 3 but only inside of the function not in the global scope.

Here there’s a disadvantage where the changes that we tend to apply to the data won’t happen globally after calling the function. Therefore, a solution is to store the output of the function in a variable and override the value of a; But it is not a great solution as you will end up with many overrides and a spaghetti code in case of a big project, while you can use pass by address instead and apply the changes on a global scope, so overriding can be a one-time thing that you can use for a small change, not for a big software.

2. Pass By Reference

Now let’s take a look at the same program with a few changes:

By running the program above, we will get an output of:

  • Initial value of a is: 42
  • Value of a inside passByReference function: 126
  • Value of a is: 126

We can notice that the initial value of a is 42 since that’s the number we assigned to the variable in the main function, then we call passByReference function and we get a print statement showing the value of a inside of the function which is 126, and then we print a after calling the function and the value changed from 42 to 126!

That’s called pass by reference or pass by address as we change the value of the variable globally. In other words, we go to the address (memory location) where the variable is stored and change its value. Therefore, the value of the variable changes across the whole program or as we can say the global scope (educative, n.d)

That’s why after calling passByAddress the new value of a became 126 as it changed globally. Here there’s a disadvantage of data loss as pass by reference changes the value of the variable in its address so the original data is lost.

References:

- educative. (n.d). Pass by value vs. pass by reference. Retrieved from https://www.educative.io/edpresso/pass-by-value-vs-pass-by-reference

--

--

Flare

I write what I needed to read in the past | Opinions are my own.