Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume we have the following program for some language with C-like syntax. int x

ID: 3825949 • 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) Using the same definition for foo that we used in question5, 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 care all call-by-name?

Explanation / Answer

Call by value: Here the value will not be modified, so x value will be the same, 1.
But as x is a global variable, x++ will increment the value to 2.
So, the value of x after the method call is 2.
Call by reference:
a = 1, b = 1, and c is passed some address next to x i.e., y value i.e., 2.
a will become 1 * 2 = 2;
b will now become 2 * 2 = 4.
x is a global is incremented to 5.
y will be modified.
So, the result after the operation is: x = 5.

Call by value/result.
If x is returned, still it will be same as in the calse of call-by-value. This is
because x value is global.
So, x is 2.

Call by name?
Still a, b, c values doesn't affect the value of x.
So, it will be 2.