Answer: On calling a function in the call by value method, we pass the values of the variables using stack. Before the function call is made, arguments are pushed to the stack. Stack will contain a duplicate variable which are usually the arguments.


int sum(int x, int y)
{
  int z = 0;
  z = x + y;
  x = 0;
  y = 0;
  return z;
}

int main()
{
  int a, b, s;
  printf("Enter two numbers");
  scanf("%d %d", &a, &b);
  s = sum(a, b);
  printf("Sum = %d", s);
}


In this example, the value of a and b are copied onto the argument variables x and y of the function sum(). The actual values of a and b will remain intact.

Here is the block diagram describing disassembly steps, call stack and argument variables. Calling sum (a, b) can be split into some assembly steps like-

  1. push value of b
  2. push value of a
  3. save return address
  4. call function

Call by value - describing disassembly steps, call stack and argument variables

Here one point to note is x and y are in stack. Value of a and b will be copied to x and y. Inside sum() value of x and y will be changed but it will not affect a and b in the main().

About our authors: Team EQA

You have viewed 1 page out of 252. Your C learning is 0.00% complete. Login to check your learning progress.

#