# GATE2001-2.17 | UGCNET-AUG2016-III: 21

3.8k 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. $\text{10, 3}$
2. $\text{31, 3}$
3. $\text{27, 7}$
4. None of the above

recategorized
2
Why it is out of syllabus  taged ?

Here, variable $x$ of func1 points to address of variable $y.$

and variables $y$ and $z$ of func1 points to address of variable $x.$

Therefore, $y=y+4 \implies y=10+4 = 14$

and $z=x+y+z \implies z=14+14+3=31$

$z$ will be stored back in $x.$  Hence, $x=31$ and $y$ will remain as it is. $(y=3)$

Answer is $31, 3$

selected by
5

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  ??

8
^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.
6
@Arjun z = x + y + z ⇒ z = 3 + 14 + 14 = 31  ??? is this right order
1
Hey, actually I can't understand which line is shows that variable x points the address of variable y???
1

In Program P1() where function has been called, the order is func1(y,x,x)}but in function definition it's func1(x,y,z). The order of x and y has been changed keeping the parameter names similar. Since parameter names could be anything inside function definition, x is pointing to address of y, y and z both are pointing to address of x.

1
in the line z=x+y+z why are we not taking x as 3, i mean to say 3+14+10=27?
1
Yes sir why not z=27 is taking
3
Since it is call by reference, so y and z both will point to x. The "y=y+4" statement changes the value of y to 14 and and hence now y and z both will point to x having value 14. Therefore we have to take y and z value as 14 in func1(x,y,z).
Program P1()
{
x = 10;
y = 3;
func1(y,x,x);
print x;
print y;
}

/* It's the call by reference. */

/* So func1(x,y,z)'s parameters x, y, z will respectively contain */
/* the address of the original parameters */

/* It means calling func1(y,x,x), */
/* the parameter x contains y, y contains x and z contains x. */

func1(x,y,z) /*  (y,x,x) = (10,3,3)  */
{
y = y + 4;   /* x = x + 4  */
/*   = 10 + 4 */
/*   = 14     */

z = x + y + z   /* x = y + x + x   */
/*   = 3 + 14 + 14 */
/*   = 31          */
}

/* Here the value of y is unaffected. */
/* Just the value of x is changed. */

$\therefore$ It's $x=31, y = 3$ as the final output.

So the correct answer is B.

edited
1 vote
z=x+y+z;  here z=3+14+14; is the right order so z=31 since func1(y,x,x) is the function call and both y and z in function definition func1(x,y,z) , point to x and x

And x point to y and in this function definition there is no change in x value means in value of y because here x point to y and there ,in function definition func1(x,y,z), is no equation for change in x, it is only contain y and z change.

i.e. func1(x,y,z)

{ y=y+4; so y=10+4;

z=x+y+z; so z=3+14+14;

}

So finally in Program P1()

print x; 31

print y; 3

## Related questions

1
2k views
Consider the following program Program P2 var n : int; procedure W(var x : int) begin x = x + 1; print x; end procedure D begin var n : int; n = 3; W(n); end begin \\begin P2 n=10; D; end If the language has dynamic scooping and parameters are passed by reference, what will be printed by the program? 10 11 3 None of the above
The following program fragment is written in a programming language that allows global variables and does not allow nested declarations of functions. global int i=100, j=5; void P(x) { int i=10; print(x+10); i=200; j=20; print (x); } main() {P(i+j);} If the ... scoping and call by name parameter passing mechanism, the values printed by the above program are $115, 220$ $25, 220$ $25, 15$ $115, 105$
Consider the program below in a hypothetical language which allows global variable and a choice of call by reference or call by value methods of parameter passing. int i ; program main () { int j = 60; i = 50; call f (i, j); print i, j; } procedure f (x, y) { i = 100; x = 10; y = y + ... ; Call by reference :$i = 100, j = 60$ Call by value : $i = 100, j = 60$; Call by reference :$i = 10, j = 70$