Call by Value Vs Call by Reference
What is difference between call by value and call by
reference in C?

What is Call by Value method?
Call by value
method copies the value of an argument into the formal parameter of that
function. Therefore, changes made to the parameter of the main function do not
affect the argument.
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.
Example of a Call by Value method
#include<stdio.h>
void
increment(int);
void main()
{
int a = 10;
printf("Before function calling: %d", a);
increment(a);
printf("After function calling: %d", a);
getch();
}
void increment(int x)
{
x = x + 1;
printf
("In function value is: %d", x);
}
Output:
Before function calling 10
In function value is 11
After function calling 10
What is
Call by Reference method?
Call by
reference method copies the address of an argument into the formal parameter.
In this method, the address is used to access the actual argument used in the
function call. It means that changes made in the parameter alter the passing
argument.
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 a Call by Value method
#include<stdio.h>
void increment(int *x);
void main()
{
int a = 10;
printf("Before function calling: %d", a);
increment(&a);
printf("After function calling: %d", a);
getch();
}
void increment(int *x)
{
*x = *x + 1;
printf
("In function value is:", *x);
}
Output:
Before function calling 10
In function value is 11
After function calling 11
Advantages of using Call
by value method
Pros/benefits of a call by
value method:
·
The method doesn't change the
original variable, so it is preserving data.
·
Whenever a function is called it, never affect the actual
contents of the actual arguments.
·
Value of actual arguments passed to the formal arguments, so
any changes made in the formal argument does not affect the real cases.
Advantages of using Call
by reference method
Pros of using call by
reference method:
·
The function can change the value of the argument, which is
quite useful.
·
It does not create duplicate data for holding only one value
which helps you to save memory space.
·
In this method, there is no copy of the argument made.
Therefore, it is processed very fast.
·
Helps you to avoid changes done by mistake
·
A person reading the code never knows that the value can be
modified in the function.
Disadvantages
of using Call by value method
Cons/Drawbacks of a call
by value method:
·
Changes to actual parameters can also modify corresponding
argument variables
·
In this method, arguments must be variables.
·
You can't directly change a variable
in a function body.
·
Sometime argument can be complex expressions
·
There are two copies created for the same variable which is
not memory efficient.
Disadvantages of using
Call by reference method
Cons of using call by
reference method:
·
Strong non-null guarantee. A function taking in a reference
need to make sure that the input is non-null. Therefore, null check need not be
made.
·
Passing by reference makes the function not pure
theoretically.
·
A lifetime guarantee is a big issue with references. This is
specifically dangerous when working with lambdas and multi-threaded programs.
Summary
of Call by Value Vs Call by Reference
|
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. |