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;
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-
push value of b
push value of a
save return address
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.