Call by Value and Call by Reference in C

Call by Value:

Call by Value is a method of passing arguments that is used to a function and copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
Example:

#include<stdio.h>
#include<conio.h>
void main()
{
 int x=25, y=10;
void add(int p, int q);
clrscr();
printf("Before function call x=%d y=%d",x,y);
add(x,y);
printf("After function call x=%d y=%d",x,y);
getch();
}
void add(int p, int q)
{
 p=p+10;
 q=q+10;
printf("Inside the function call x=%d y=%d",x,y);
}

Call by Reference:

It permits us to pass parameters to the function by reference. When we pass the arguments by reference, the formal arguments in the called function becomes aliases to the actual arguments in the calling function that means when the function is working with its own arguments.
Example:

#include<stdio.h>
#include<conio.h>
void main()
{
 int x=25, y=10;
void add(int p, int y);
clrscr();
printf("Before function call x=%d y=%d",x,y);
add(&x,&y);
printf("After function call x=%d y=%d",x,y);
getch();
}
void add(int *x,int *y)
{
 *x=*x+10;
 *y=*y+10;
printf("Inside the function call x=%d y=%d",*x,*y);
}

Actual Arguments:

The arguments listed in the function calling statement that is called Actual Arguments.
Example:
argu

Formal Arguments:

The arguments used in the function declaration that is called Formal Arguments.
Example:
argu2

We can Difference between Call by Value and Call by Reference given below:




Call by Value Call by Reference
1. It is a usual method to call a function in which only the value of the variable is passed as an argument. 1. In this method, the address of the variable is passed as an argument.
2. Memory location occupied by formal and actual arguments is different. 2. Memory location occupied by formal and actual arguments is same.
3. Any alteration in the value of the argument passed is local to the function and it's not accepted in the calling program. 3. Any alteration in the value of the argument passed is local to the function and it is accepted in the calling program.
4. This method is slow.4. This method is fast.
5. There is no possibility of wrong data manipulation because the arguments are directly used in an expression. 5. There is a possibility of wrong data manipulation because the addresses are directly used in an expression.