0
int f(float i, float j, float k)
{
  float x=i;
  i=j;
  j=k;
  k=x;}

main(){
  float x=5; y=10 z=25;
  if(...){
     f(x,y,z);
  }
  else{
     f(y,z,y); /* what's x,y,z after this line using call-by-reference and call-by-name mechanism*/
  }
 }

I think I have a pretty good understand about these three passing mechanism.

After f(x,y,z). x,y,z will have value

10 25 5 if it is call by reference

5 10 25 if it is call by value

10 25 5 if it is call by value result.

(I double checked answer, I am sure those are corrent)

but my answer is completely wrong for f(y,z,y). I got 10,25,10 for call by reference and call by name. How would value of x,y,z change after f(y,z,y) if it is using call by reference and call by name

user2247155
  • 19
  • 1
  • 1
  • 6

1 Answers1

0

for the "call by reference" for f(y,z,y), first let us rewrite the code of the f function

int f(float & i, float & j, float & k)
{
   float t=i;
   i=j;
   j=k;
   k=t;
 }

(I renamed the local variable x to t to avoid confusions)

Calling f(y,z,y) amounts to the following sequence:

 {
  float t=y;
   y=z;
   z=y;
   y=t;
 }

with the initial values y=10 z=25, so

   float t=10;
   y=25;
   z=25;
   y=10;

ending with z=25, y=10 and x unchanged.

Michel Billaud
  • 1,758
  • 11
  • 14