Wednesday, October 13, 2010

What is Call By Value in C

,


In C Programmings we have different ways to parameter passing schemes such as Call by Value and Call by Reference.

Call By Value:- In the Call By Value the function is to be called by passing the value in the parameter of a function called. By default, C programming language uses call by value method to pass arguments.

There are the two type of a arguments which are passed in the function.

1) Formal Arguments:- Formal Arguments is the parameters which are passed in th function where the function body define.

2) Actual Arguments - Actual Arguments are those arguments which are passed in the function ,where the function called.

Why we use call By Value:-  In the Call By value when a function is called then the actual arguments are used and the value is passed in this argument.The control transfers to the function body then these values of actual arguments are being copied to the formal arguments and further processing start.

So there values are local to this block and does not take effect in the actual arguments. So that once the control get back to the main block then these values are completely vanished.These whole procedure are occurred in call by value. In below example we are swapping values with Call By Value


Example:-


/* function definition to swap the values */
void swap(int a, int b)
{
   int xx;
   xx = a;
   a = b;
   b = xx;
   return;
}

#include <stdio.h>
#include <conio.h>

void swap(int a, int b);
int main ()
{
   int m = 20;
   int n = 10;
   printf("Before swap, value of m : %d\n", m );
   printf("Before swap, value of n : %d\n", n );

   swap(m, n);

   printf("After swap, value of m : %d\n", m );
   printf("After swap, value of n : %d\n", n );

   return 0;
}

0 comments to “What is Call By Value in C”

Post a Comment