Assume we have the following program for some language with C-like syntax. Int x
ID: 3825165 • Letter: A
Question
Assume we have the following program for some language with C-like syntax. Int x, y, z; void foo (a, b, c) {a = a * 2; b = b * 2; x++; c = c + b;} x = 1; y = 2; z = 3; foo(x, y, z); What are the values of x, y, and z after the call to foo for each of the following cases? a) The formal parameters a, b, and c are all call-by-value? b) The formal parameters a, b, and c are all call-by-reference? c) The formal parameters a, b, and c are all call-by-value/result (i.e. in/out mode in Ada)? d) The formal parameters a, b. and c are all call-by-name?Explanation / Answer
1) Call by value
x = 1, y = 2, z = 3
as these are called by value hence the value change in formal parameter won't be visible to actual parameter.
Now as these are defined global the function increases the value of x as x++
Hence x becomes 2, y = 2 and z = 3
2) Call by reference
Changes in formal will reflect in actual
x = 1 y =2 z = 3
After function call
a = 1 b = 2 c= 3
a = a*2
a= 1* 2
a = 2
b = b* 2
b= 2*2
b= 4
c= c+b
c = 3+4
c= 7
So now x = 2, y = 4 and z = 7
3) Call by name : whole function definction replces the function call
So again it doesn't change the value of actaul paramets hence the values of x = 1 y =2 and z = 3