743 views

What is printed by following program, assuming call-by reference method of passing parameters for all variables in the parameter list of procedure P?

program     Main(inout, output);
var     a, b:integer;
procedure P(x, y, z:integer);
begin
y:=y+1
z:=x+x
end P;
begin
a:=2; b:=3;
p(a+b, a, a);
Write(a)
end.


retagged | 743 views

let variable "$a$" has address $100$  and "$b$" has $200$ .

and a variable in which "$a+b$" is stored has address $300$.

now $p(300,100,100)$  which represent $x,y,z$

$y:=y+1$  // it makes $a=3$;

$z:=x+x$ //  x means the value contained at address $300$ i.e. $5$

$5+5 =10$  hence value at address $100$ i.e.  variable "$a$" will get the value $10$ .

hence value of a i.e. $10$ will be printed.
by Junior (843 points)
edited by
0
if it is pass by reference then why does procedure p mentions integer after z?
0
nice explanation @psb
+1

@

I could not understand your solution. The variable 'a' is stored at location 100 whose value is updated to 3 by the code. Value stored at location 300 is updated to 10 which does not represent variable 'a', rather than it represented 'a+b'. So how 'a' got value 10?

0
same problem i coudn't understand properly at last  value of a=3,b=10,c=5

so here write (a) will print 3 clear

where i missed ?
+1
Value should be 3 or 10 but z write it(a)  last so answer 10.