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

Consider the following pseudo-code. For each of the following parameter passing

ID: 3761031 • Letter: C

Question

Consider the following pseudo-code. For each of the following parameter passing mech- anisms, give the value returned for every function call, in the order that the returns occur. Then give the output produced by the program. Assume parameters are evaluated left-to-right and their values are copied back left-to-right, where appropriate. For example, if f3 is the first function to return and it returns a value 17, simply write something like “f3 returns 17.”

int p = 3;

int q = 2;

int f1(int b, int c) {

b = 2 * c;

c = 3 + b;

return b + c;

}

Int f2(int a, int b) {

return f1(a,a) * f1(b, a);

}

print f1(p,q) + f2(p, f1(p,q));

a. Value b. Reference c. Value-result

Explanation / Answer

The program is explained here with comments.

The conclusion is the code prints a value 236.

int p = 3;               /*p is assigned a value 3.*/
int q = 2;               /*q is assigned a value 2.*/
int f1(int b, int c) {   /*Call by value. function.*/
b = 2 * c;               /*The value of c is doubled and stored into b.*/
c = 3 + b;               /*The then value of b is incremented by 3.*/
return b + c;           /*Now the summation of b and c is returned.*/
}
Int f2(int a, int b) {   /*Call by value. function.*/
return f1(a,a) * f1(b, a);   /*f1 is called with values a, a. f1 is again called with values b, a. Now both the values are multiplied and returned.*/
}
/*f1(3 ,2) is called first, with values 3, 2. So, the steps include:
b = 2 * c, means b value will become 2 * 2, which means 4.
c = 3 + b, means c value will become 3 + 4, which means 7.
Then the summation of b and c, which means 4 + 7 i.e., 11 is returned. */
/*On the right side of the operator, f2(p, f1(p,q)) is called.
Which means the function is called with values f2(3, 11).
f2 inturn calls the function f1(3,3), and f1(11, 3) and this is multiplied and returned.
f1(3, 3) steps include:
b = 2 * 3, which means 6.
c = 3 + b, which means 9.
So, f1(3, 3) returns 15.
f1(11, 3) steps include:
b = 2 * 3, which means 6.
c = 3 + 6, which means 9.
So, f1(11, 3) also returns 15.*/
/*So f2(3, 11) = f1(3, 3) * f1(11, 3) = 15 * 15 = 225.*/
/*Finally, f1(3, 2) + f2(3, f1(3, 2)) = 11 + f2(3, 11) = 11 + 225 = 236.*/
print f1(p,q) + f2(p, f1(p,q));  
a. Value b. Reference c. Value-result