Signup/Sign In
LAST UPDATED: JANUARY 31, 2023

Difference Between Call By Value and Call By Reference

In this tutorial, we will learn the difference between Call by value and Call by reference.

Call By Value

Difference Between Call By Value and Call By Reference

Only the "value" of a basic data type (integer, character, or string) is provided to the function code when you send it to a function/method. A value of an argument is copied into a 'formal parameter' of the function code by the function. If the formal parameter in a function code is changed, the original value of the argument that is used to invoke that function is not changed.

Simply said, when a function/method is called using the 'call by value' technique, a copy of the variable is supplied to the function code. If a function code modifies the value of the duplicate of the variable, it has no effect on the variable's original value.

The key advantage of using the call by value approach is that the data provided via variables has no effect on the data itself. The reason for this is that changes made to variables within functions only affect the function copy of the parameter value, leaving the original value of the argument untouched.

Call By Reference

The Call by Reference technique delivers an argument's reference/address to the function code. Because the address of an argument is supplied to the function code, a 'pointer' variable would be the formal parameter taking that address. Now that the address of an argument has been retrieved by function code, any change in the value of an argument will likewise change the original value of an argument.

In C++ and Java, passing an object to a function or method is quite frequent, and objects are always given by reference. The object used to call the function/method is affected by changes made to the object within the function/method.

Reference variables are used in the reference method, allowing us to send arguments to the function. Calling a function by reference produces an alias of the formal parameters to the real arguments in this method.

As a result, the called function does not make its own copy of the data. Instead, it uses reference names to relate to the original data. As a result, the function works with raw data, and any changes made to the raw data are mirrored in the referenced data.

Comparison Table Between Call By Value And Call By Reference

Call By Value Call By Reference
  • A function that allows data or values to be passed to other functions.
  • A function that allows data or values to be passed to other functions.
  • Programming languages that are based on the C programming language.
  • C++ and Java are C-based programming languages, but not C itself.
  • To provide parameters to a different function.
  • To provide parameters to a different function.
  • Actual arguments are copied and sent to the appropriate formal arguments.
  • The formal arguments are given a reference to the location or address of the actual arguments.
  • The personal copy is where the changes are done. Changes done inside a function do not affect other functions.
  • Any modifications to the formal arguments will be reflected in the actual arguments. Changes done inside the function are also mirrored outside of it.
  • The original value has not been changed.
  • The original value has been changed.
  • Actual and formal arguments will be stored in separate locations in memory.
  • In the same memory region, actual and formal arguments will be formed.

You May Also Like:



About the author:
Adarsh Kumar Singh is a technology writer with a passion for coding and programming. With years of experience in the technical field, he has established a reputation as a knowledgeable and insightful writer on a range of technical topics.