in Programming in C edited by
1,480 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
in Programming in C edited by
1.5k views

1 Answer

3 votes
3 votes
Best answer

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
by

3 Comments

@arjun sir, call by value result point 3 is  5 right ?

as b is passed as argument to test1 with formal parameter a , while returning from the function test1 value of a is copied back to b . which makes b from 10 to 5.
1
1
yes, thats 5. Corrected now :)
0
0
call by value:

point 1: 5 , 3

point 2 : 5,10

point 3: 10
1
1

Related questions