The Gateway to Computer Science Excellence
+12 votes
698 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.
in Compiler Design by Veteran (105k points)
retagged by | 698 views

1 Answer

+15 votes
Best answer
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.

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
50,648 questions
56,459 answers
195,337 comments
100,196 users