Assume we have the following program for some language with C-like syntax. int x
ID: 3825169 • 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(times, y, z); Using the same definition for foo that we used in question 5, assume we make the following call. x = 1; foo(x, x, x + 1); What is the value of x 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
Formal parameters changes won't be reflected in actual paramter
Hence x becomes 2 as there is x++ in function and x is global variable.
2) Call by reference
a gets x
a = 1
b gets x
b = x
c = x+1
c = 2
here changes in a & b will be reflected to x as they are directly refernced to x
a = a* 2
a = 1*2
a =2
b = b*2
b = 1*2
b = 2
these values will be reflected to x x becomes 2
Now we have x++
so x becomes 3
After function call x will be 3
3) Call by name : in this whole definition will replcae function call hence changes in formal will not reflect to actual
Value of x becomes 2 as there is x++ in function