CALL BY VALUE AND CALL BY REFERENCE

In c the terms, “call by value ” and “call by reference” refers to different mechanigium of passing arguments to function.

Call by value

1.in call by value a copy of argument value his pass through the function.

2.the original variable outside the function remains unaffected by any modifications made to the parameters inside the functions.

3.changes mode to the parameters inside the functions are not reflected outside the function.

4.the function works with its own local copy of the arguments.

5.primitive data type like integers float character are usually passed by value.

Ex

void swap(int a, int b)

{

  int t=a;

  a=b;

  b=t;

}

int main()

{

  int x=10;

  int y=20;

  swap(x,y);

  printf(“%d%d”, x,y);      

  return 0;

}

call by reference

1.in call by reference, the memory address of the argument is passed to the function.

2.modification made to the parameter inside the function affect the original variable outside the function.

3.function works directly with memory location of the arguments.

4.pointers are the typically used to implement call by reference in c.

Ex

void swap(int*x,int*y)

{

  int t=*a;

  *a=*b;

  *b=t;

}

int main()

{

  int a=5,b=10;

  swap(&a,&b);

  printf(“%d%d”,a,b);      //o/p   10  5

  return 0;

}