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;
}
10 
11  int main()
12  {
13    int a, b, s;
14    printf("Enter two numbers");
15    scanf("%d %d", &a, &b);
16    s = sum(a, b);
17    printf("Sum = %d", s);
18  }
19 
20 
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().

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

 Vote 0

Similar topics related to this section

function declaration and definition, calling conventions, calling convention of C,C++ and PASCAL, library function linking, call-by-value and call-by-reference, parameter passing call-by-value, parameter passing call-by-reference, structure as function argument, array as function argument, inline-functions,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan