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: 3826806 • 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

a) Call by value:

x=2;

b) Call by reference:

void foo(a,b,c)
{
   a = a*2; //x=2
   b = b*2;//x=4
   x++; // x = 5
   c = c+b;//x=2+5 = 7
}

x=7

c) Call by value/result:
void foo(a,b,c)
{
   a = a*2; //a=2
   b = b*2;//b=4
   x++; // x = 2
   c = c+b;//c=2+4 = 6
}
x=6;

d) Call by name:

void foo(a,b,c)
{
   a = a*2; //x=2
   b = b*2;//x=4
   x++; // x = 5
   c = c+b;//c=6+5 = 11
}
x=11;