In call by value a copy of actual arguments is passed to formal arguments of the called function and any change made to the formal arguments in the called function have no effect on the values of actual arguments in the calling function.

Example:

#include <stdio.h>
 
void swapByValue(int, int); /* Prototype */
 
int main() /* Main function */
{
  int n1 = 10, n2 = 20;
 
  /* actual arguments will be as it is */
  swapByValue(n1, n2);
  printf("n1: %d, n2: %d
", n1, n2);
}
 
void swapByValue(int a, int b)
{
  int t;
  t = a; a = b; b = t;
}
 
OUTPUT
======
n1: 10, n2: 20

 

In call by reference, the location (address) of actual arguments is passed to formal arguments of the called function. This means by accessing the addresses of actual arguments we can alter them within from the called function.

 

Example:

#include <stdio.h>
 
void swapByReference(int*, int*); /* Prototype */
 
int main() /* Main function */
{
  int n1 = 10, n2 = 20;
 
  /* actual arguments will be altered */
  swapByReference(&n1, &n2);
  printf("n1: %d, n2: %d
", n1, n2);
}
 
void swapByReference(int *a, int *b)
{
  int t;
  t = *a; *a = *b; *b = t;
}
 
OUTPUT
======
n1: 20, n2: 10