Translate

Wednesday 9 April 2014

Difference Between Call By Value And Call By Reference

Call By Value:-

      call by value passes the copy of the variable to the function that process it

      Let’s take a look at a call by value example:
  
#include <stdio.h>
#include<conio.h>
 
void call_by_value(int x) 
{
       printf("Before adding 10 x = %d .\n", x);
       x=x+10;
       printf("After adding 10 x = %d.\n", x);
}
 
void main() 
{
  int a=10;
  clrscr();
  printf("a = %d before function call_by_val.\n",a);
  call_by_value(a); 
  printf("a = %d after function call_by_val.\n",a);
  getch();
}



Call By Reference:-

    call by reference passes the memory reference of the variable (pointer)
    to the function. 
Let’s take a look at a code example:
#include <stdio.h>
#include<conio.h>
 
void call_by_reference(int *y) 
{
      printf("Before adding 10 y = %d .\n", *y);
      (*y) += 10;
      printf("After adding 10 y = %d .\n", *y);
}
 
void main() 
{
      int b=10;
      clrscr();
      printf("b = %d before function call_by_ref.\n",b);
      call_by_reference(&b);
      printf("b = %d after function call_by_ref.\n",b);
}

No comments:

Post a Comment