The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+6 votes
1.7k 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
asked in Compiler Design by Veteran (59.5k points)
edited by | 1.7k views
+1
Why it is out of syllabus  taged ?

1 Answer

+16 votes
Best answer
Answer is B.

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$
answered by Loyal (8.2k points)
selected by
+1

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

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.

0
in the line z=x+y+z why are we not taking x as 3, i mean to say 3+14+10=27?
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

37,111 questions
44,694 answers
127,237 comments
43,753 users