The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+5 votes
1.2k views

What is printed by the print statements in the program P1 assuming call by reference parameter passing?

Program P1()
{
    x = 10;
    y = 3;
    func1(y,x,x);
    print x;
    print y;
}

func1(x,y,z)
{
    y = y + 4;
    z = x + y + z
}
  1. 10, 3
  2. 31, 3
  3. 27, 7
  4. None of the above
asked in Programming by Veteran (68.9k points)
edited by | 1.2k views

1 Answer

+14 votes
Best answer
ans is B.

here, variable x of func1 points to address of variable y.

and variable y and z of func1 points to address f variable x.

therefore y=y+4 => y=10+4 = 14

and z=x+y+z => z=14+14+3=31

z will be stored back in x  hence x=31 and y will remain as it is hence y=3

ans is 31, 3
answered by Boss (8.5k points)
selected by

should it look like ??
 

Program P1()
{
    x = 10;
    y = 3;
    func1(&y,&x,&x)
    print x;
    print y;
}

func1(*x,*y,*z)
{
   * y = *y + 4;
    *z = *x +* y +* z
}


AS IT IS CALL BY REFERRENCE  ??
^Yes, the result should be same - but change to func1(&y, &x, &x);

But in theory you are using call by value- and the value being a pointer. Using '*' operator this is making the effect of call-by-reference.
@Arjun z = x + y + z ⇒ z = 3 + 14 + 14 = 31  ??? is this right order
Hey, actually I can't understand which line is shows that variable x points the address of variable y???
Answer:

Related questions



Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

32,693 questions
39,293 answers
110,106 comments
36,700 users