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: 3825164 • 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); What are the values of x, y, and z 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


=====================================================
a) The formal parameters a,b and c are all call-by-value?

---------
Answer:
---------

   x = 2
   y = 2
   z = 3
  
--------------
Explanation:
--------------

   call-by-value copies the values of x,y,z to a,b,c
Any changes to call-by-value does not effect x,y,z values.
In the given function x=1,y=2,z=3 are copied to a=1,b=2,c=3

   The values of a,b,c in the function execution.
   a = a*2 = 1*2 = 2
   b = b*2 = 2*2 = 4
   x++ = 2
   c = c+b = 3+4 = 7
  
   In function due to x++ is present it incremented the x value to 2.
   Hence x becomes 2 and y,z remains the same.
  
=====================================================
b) The formal parameters a,b and c are all call-by-reference?


---------
Answer:
---------

   x = 3
   y = 4
   z = 7
  
--------------
Explanation:
--------------

   call-by-reference refers the address of x,y,z to a,b,c
Any changes to call-by-value does not effect x,y,z values.
In the given function x=1,y=2,z=3 are referred to a=1,b=2,c=3

   The values of a,b,c in the function execution.
   a = a*2 = 1*2 = 2
   b = b*2 = 2*2 = 4
   x++ = 3 (Since x value is here a value i,e 2 and is incremented to 3)
   c = c+b = 3+4 = 7
  
   Here x value is 3 , y value is 4 (since b represents y)
   z value is 7(since c represents z)

=====================================================
c) The formal parameters a,b and c are all call-by-value/result?

---------
Answer:
---------

   x = 2
   y = 2
   z = 3
  
--------------
Explanation:
--------------

   call-by-value/result copies the values of x,y,z to a,b,c. It is similar to call-by-value.
Any changes to call-by-value/result does not effect x,y,z values.
In the given function x=1,y=2,z=3 are copied to a=1,b=2,c=3

   The values of a,b,c in the function execution.
   a = a*2 = 1*2 = 2
   b = b*2 = 2*2 = 4
   x++ = 2
   c = c+b = 3+4 = 7
  
   In function due to x++ is present it incremented the x value to 2.
   Hence x becomes 2 and y,z remains the same.
  
  
=====================================================
d) The formal parameters a,b and c are all call-by-name?


---------
Answer:
---------

   x = 3
   y = 4
   z = 7
  
--------------
Explanation:
--------------

   call-by-name refers the address of x,y,z to a,b,c. But address remains the same.
Any changes to call-by-value does not effect x,y,z values.
In the given function x=1,y=2,z=3 are referred to a=1,b=2,c=3

   The values of a,b,c in the function execution.
   a = a*2 = 1*2 = 2
   b = b*2 = 2*2 = 4
   x++ = 3 (Since x value is here a value i,e 2 and is incremented to 3)
   c = c+b = 3+4 = 7
  
   Here x value is 3 , y value is 4 (since b represents y)
   z value is 7(since c represents z)  
=====================================================