CMPSC 461 Homework 3 Solutions. Prof. G. Tan
Q1
a)foo(y,z);passbyvalue: x=4,y=2,z=1 b)foo(y,z);passbyreference: x=4,y=1,z=1 c) foo(y,z); pass by value-result: x = 4, y = 1, z = 1 d)foo(x,y);passbyreference: x=3,y=2,z=1 e)foo(x,x);passbyreference: x=0,y=2,z=1
f) foo(x,x); pass by value-result: x = 3, y = 2, z = 1
Q2
(a) For foo1: sum = 5
Initially sum = 3.
Call function bar with argument x = 3
bar modifies x to be 1 and returns 2*1 = 2. So, sum = 3 + bar(x) = 3 + 2 = 5.
(b) For foo2: sum = 0
Call to function bar with argument x = 2
Function bar changes x to value 0 and returns value 2*0 = 0. So, sum = 0. Then sum = sum + x = 0 + 0 = 0.
Q3
Step 1
Step 2
Step 3 Step 4
Step 3 shows the stack of activation records before line 12 of the program¡¯s execution. Step 4 shows the stack of activation records before line 6 of the program¡¯s execution.