Difference Between Call by Value and Call by Reference in C++

Video Tutorial
FREE
Pass by reference thumbnail
This video belongs to
C++ Course: Learn the Essentials
14 modules
Certificate
Topics Covered

One of the most fundamental concepts in C++ is the concept of passing arguments to functions. For a function to perform a specified task, it must be "called". In C++, a function can be called in two ways: call by value and call by reference.

Before the concepts of Call by Value and Call by Reference in C++, learn about what a function in C++ is.

What is Call by Value in C++?

Call by value is also known as pass by copy or call by value. By definition, pass by value means to pass in the actual parameter’s copy in memory as the formal parameters.

Let’s understand what happens when we pass a value to a function via pass by value method:

  • We pass in the actual parameters.
  • A copy of the same is created in the memory.
  • This copy is passed as the formal parameters.

Now since we are passing a copy, both the parameters are stored in different memory locations. Thus, any change inside the function will not have any effect on the original values.

Example:

Output:

The above function aims to swap the values of the variables x and y. However, we did not see any change in the values. This is because all the changes are made in the dummy variables (a and b in this case) that are stored at a different memory location.

difference between pass by value and pass by reference

Now recall the program to multiply a number by 5 that we first encountered! Are you now able to reason the logic behind the unexpected answer?

When to use Call by Value in C++?

  • When we want to perform calculations without changing the original values.
  • To prevent the values from changing via any other thread while multithreading (Multithreading in simple words is running two or more parts of a program concurrently.)

Advantages of Call by Value in C++

  • Functions are free to modify the passed values without affecting the original data.
  • Great while working with multithreaded or asynchronous programs where we need to keep track of the values.
  • Pass by value method can be used to convert a string from an integer without modifying its value.

Disadvantages of Call by Value in C++

  • Since pass-by-value is implemented by passing a copy of the data, the data size increases (doubles). This may not be an issue with smaller programs but can cost efficiency in larger programs.
  • If the argument is too large, copying the values can take a significant amount of time.
  • There is no way to propagate back the updated values through the parameters.

What is Call by Reference in C++?

Call by reference is also known as pass-by address or call by reference. While calling a function, instead of passing the values of variables, we pass a reference to values.

let’s understand what happens when we pass a Reference to a function via pass-by Reference:

  • We pass in the actual parameters.
  • A reference variable is created and passed to the function.
  • A reference variable is a nickname for any variable.

Now since we are passing a copy of the address, both the variables point to the same value. And thus, any change in the function would be reflected in the actual values.

Example:

Output:

In the above C++ program, both the actual and the formal parameters point to the same memory location. In other words, both x and a are aliases of the same memory location. The same goes for y and b. Thus, all the changes made in the swap() function are evident in the original values.

The variables a and b contain the address of x and y respectively.

Notice we are using & when calling the function. It is used to denote that an address is passed through the parameters.

In the function definition, we are using * (dereferencing variable) to access the value at the given memory location. This means that we can access the values stored at x (5) and y (15) using *a and *b respectively. In simpler terms, *a = 5 and *b = 15.

pass by value vs pass by reference

Note that if we fail to use the dereferencing operator, we might encounter an error saying, assigning to int from incompatible type int* dereference with *, which means that we cannot assign int* to int.

Now let’s recall the very first example that we encountered. Can you now tweak it to produce the expected answer, 10? Here is how you can do it:

The output of this program would be 10. Here, we have only changed the method of passing the values and not any variable name.

When to use Call by Reference in C++?

  • When we are changing the parameters passed by the client program.
  • When passing large structures and classes to make the program efficient.
  • To avoid copying the values and reducing the space complexity.

Advantages of Call by Reference in C++

  • We can produce efficient programs since no copy of the data is made.
  • It enhances the ease of exchanging information between functions through the use of parameters.

Disadvantages of Call by Reference in C++

  • This method is not recommended while developing multithreaded programs since it can cause loss of data.
  • Being a high-level mechanism, pass-by reference can only be used within a single program.

call by value vs call by reference

Difference Between Call by Value and Call by Reference in C++

Call by ValueCall by Reference
Also known as pass by copy and call by value.Also known as pass by address and call by reference.
Copies of the original values are passed to the function.Only a reference variable (alias name) is passed to the function.
Any change in the function will not be reflected in the original values.Every change in values is reflected in the actual values.
Values are passed just like it’s done for any program.Pointer variables are passed to refer to the address location.

Conclusion

In this article, we have discussed in detail call by value and Call by reference. Below are the takeaways from this article:

  • There are two ways to pass a value to a function: pass by value and pass by reference.
  • In Call by value, a copy of the original values is passed to the function.
  • In Call by reference, a reference to the variable is passed to the function.
  • Both Call by value and Call by reference have their own sets of advantages and disadvantages. Using both of them depends on the requirements of the program.
  • We have also drawn a comparison between both methods to help you better understand the concepts.