if it is pass by reference then why does procedure p mentions integer after z?

The Gateway to Computer Science Excellence

+15 votes

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.

+16 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.

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.

+1

@psb

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?

- All categories
- General Aptitude 1.9k
- Engineering Mathematics 7.5k
- Digital Logic 2.9k
- Programming and DS 4.9k
- Algorithms 4.4k
- Theory of Computation 6.2k
- Compiler Design 2.1k
- Databases 4.1k
- CO and Architecture 3.4k
- Computer Networks 4.2k
- Non GATE 1.4k
- Others 1.4k
- Admissions 595
- Exam Queries 573
- Tier 1 Placement Questions 23
- Job Queries 72
- Projects 18

50,737 questions

57,317 answers

198,368 comments

105,105 users