What is call by value and Call by Reference method Example of a call by value and call by reference method Call by Value vs Call by Reference Advantages of using Call by value and Call by reference method Disadvantages of using Call by value method and Call by reference method
What is Call by Value method?
In this parameter passing method, values of actual parameters are copied to function's formal parameters, and the parameters are stored in different memory locations. So any changes made inside functions are not reflected in actual parameters of the caller.
What is Call by Reference method?
In this method, the memory allocation is the same as the actual parameters. All the operation in the function are performed on the value stored at the address of the actual parameter, and the modified value will be stored at the same address.
Example of call by value method
#include <stdio.h>
int increment(int var)
{
var = var+1;
return var;
}
int main()
{
int num1=20;
int num2 = increment(num1);
printf("num1 value is: %d", num1);
printf("\nnum2 value is: %d", num2);
return 0;
}
Output
num1 value is: 20
num2 value is: 21
Example of a Call by Reference method
#include <stdio.h>
void increment(int *var)
{
/* Although we are performing the increment on variable
* var, however the var is a pointer that holds the address
* of variable num, which means the increment is actually done
* on the address where value of num is stored.
*/
*var = *var+1;
}
int main()
{
int num=20;
/* This way of calling the function is known as call by
* reference. Instead of passing the variable num, we are
* passing the address of variable num
*/
increment(&num);
printf("Value of num is: %d", num);
return 0;
}
Output
Value of num is: 21
Parameters | Call by value | Call by reference |
---|---|---|
Definition | While calling a function, when you pass values by copying variables, it is known as "Call By Values." | While calling a function, in programming language instead of copying the values of variables, the address of the variables is used it is known as "Call By References. |
Arguments | In this method, a copy of the variable is passed. | In this method, a variable itself is passed. |
Effect | Changes made in a copy of variable never modify the value of variable outside the function. | Change in the variable also affects the value of the variable outside the function. |
Alteration of value | Does not allow you to make any changes in the actual variables. | Allows you to make changes in the values of variables by using function calls. |
Passing of variable | Values of variables are passed using a straightforward method. | Pointer variables are required to store the address of variables. |
Value modification | Original value not modified. | The original value is modified. |
Memory Location | Actual and formal arguments will be created in different memory location | Actual and formal arguments will be created in the same memory location |
Safety | Actual arguments remain safe as they cannot be modified accidentally. | Actual arguments are not Safe. They can be accidentally modified, so you need to handle arguments operations carefully. |
Advantages of using Call by value method
Advantages of using Call by reference method
Disadvantages of using Call by value method
Disadvantages of using Call by reference method