In a programming language, one can invoke functions in two ways: known as Cell by Value and Call by Reference. The general basis of differentiation in these two ways is the type of values that passes through them in the form of parameters.
The parameters that pass to the functions are known as actual parameters, and the parameters that the functions receive are known as formal parameters. Now before we dig into the difference between call by value and call by reference, let’s know a bit more about these two.
What is Call by Value?
In this particular parameter passing method, the values of the actual parameters copy into the function’s formal parameters. It stores both types of parameters in different memory locations. Thus, if one makes any changes inside the function- it does not show on the caller’s actual parameters.
This method passes a copy of an actual argument to the formal argument of any called function. In the case of a Call by Value method, any changes or alteration made to the formal arguments in a called function does not affect the overall values of an actual argument. Thus, all the actual arguments stay safe, and no accidental modification occurs to them.
What is Call by Reference?
In this case, both the formal and actual parameters refer to a similar location. It means that if one makes any changes inside the function, it gets reflected in the caller’s actual parameters.
This method passes the address or location of the actual arguments to the formal arguments of any called function. It means that by accessing the actual argument’s addresses, one can easily alter them from within the called function. Thus, in Call by Reference, it is possible to make alterations to the actual arguments. Thus, the code needs to handle the arguments very carefully. Or else, there might be unexpected results and accidental errors.
Difference Between Call by Value and Call by Reference
|Parameter||Call By Value||Call By Reference|
|Convention of Naming||In this case, the parameter’s value passes for invoking the function. In the case of calling a function, we pass the values of variables directly to a function. Thus, it has its name as Call by Value.||In this case, the parameter’s reference passes for the function invocation. Whenever calling a function, rather than passing the variables’ values, we pass its address instead (location of variables) to the function. Thus, it has its name as Call by Reference.|
|Effects of Changes||It copies the value of a passed parameter into the function’s argument. Thus, any changes that occur in any argument inside the function have no reflection on the passed parameter.||Both the passed parameter and the argument refer to a similar location. Thus, any changes occurring in any argument inside the function also reflects in the passed parameter.|
|Type of Passing||The method of Call by Value passes a copy of the variable. Here, the values of all the variables copy into their corresponding dummy variables, also called functions.||The method of Call by Reference passes the variable itself. Here, it copies the address of the actual variables in the calling function into the dummy variables called functions.|
|Memory Location (Referred)||The memory location referred to by the actual arguments and passed parameters of a function are different. Meaning, it creates the formal and actual arguments in different memory locations.||The memory location referred to by the actual arguments and passed parameters of a function are the same. Meaning, it creates the formal and actual arguments in the very same memory location.|
|Language Supported||Languages like C++, C#. PHP, Visual Basic NET, etc., provide support to the Call by Value and use it as their default method.||Only the JAVA language supports the Call by Reference method in programming.|
|Value Modification||In the Call by Value method, there is no modification in the original value.||In the Call by Reference method, there is a modification in the original value.|
|Internal Implementation||In the case of Call by Value, when we pass the value of the parameter during the calling of the function, it copies them to the function’s actual local argument.||In the case of Call by Reference, when we pass the parameter’s location reference/address, it copies and assigns them to the function’s local argument. Thus, both the actual argument and passed parameters refer to one similar location.|
|Method of Passing||The values of the variables in a Call by Value method pass using a straightforward method or a Simple technique.||Defining the pointer variables in a Call by Reference method is a prerequisite for storing the address of all the variables.|
|Manipulation of Variables||Using this method, any changes occurring to the dummy variables in any called function have no effect on the actual variable’s values in the calling function. Thus, you cannot alter or manipulate the actual variables’ values using the function calls.||Using this method, one can directly use the addresses to access the actual variables. Thus, any user can easily alter or manipulate the variables’ values through the function calls.|