edited by
1,471 views
3 votes
3 votes

Consider the following program in pseudo-Pascal syntax. What is printed by the program if parameter $a$ in procedure $\text{test1}$ is passed as

  1. call-by-reference parameter

  2. call-by-value-result parameter

program Example (input, output)
var b: integer;
procedure test2:
begin b:=10; end
procedure test1 (a:integer):
begin 	a:=5;
        writeln ('point 1: ', a, b);
        test2;
        writeln ('point 2: ', a, b);
end
begin (*Example*)
b:=3; test1(b);
writeln('point3: ', b);
end
edited by

1 Answer

Best answer
3 votes
3 votes

1. Call-by-reference:

  • point $1: 5 \; 5 $
  • point $2: 10 \;10$
  • point $3: 10$

2. Call-by-value-result

  • point $1: 5\; 3$
  • point $2: 5 \;10$
  • point $3: 5$

In call by reference there is no copying of memory location of variable and hence $‘a\text{'}$ in $\text{test1}$ is an alias (same memory location) to global variable $b$. In call-by-value-result, while passing to a function a copy of variable is created and while returning back, the final value is copied back. So, with call-by-value-result, $a$ in $\text{test1}$ is having a separate memory location than $b$. 

edited by

Related questions

13 votes
13 votes
2 answers
4
Kathleen asked Sep 15, 2014
8,186 views
The results returned by function under value-result and reference parameter passing conventionsDo not differDiffer in the presence of loopsDiffer in all casesMay differ i...