506 views
0 votes
0 votes

What will be the second value printed by the program if parameter passing mechanism is call by reference?

int b=10  //global 
begin 

procedure func(int x,int y)
begin 
    print(b)
    x=x+b
    y=y+b
end

int a=10;
int b=20;
func(a,a);
print(a);
end

1 Answer

Best answer
2 votes
2 votes

Static scoping: Here if a variable is not in local scope, it is looked in global scope. 

So, b in func() is the global b. So, 10 is printed. a is incremented by 10 when x = x + b, (due to pass by reference) and a is again incremented by 10 when y = y + b. So, print(a) will print 30.

Dynamic scoping: Here if a variable is not in local scope, it is looked in the function which called the current one. 

So, b in func() is the b from main. So, 20 is printed and a is incremented two times by 20 and final print(a) will print 10 + 20 + 20 = 50.

selected by

Related questions

1 votes
1 votes
1 answer
1
neha singh asked Aug 26, 2016
899 views
Program P1() { x=10, y=3; func1(y,x,x); print x; print y; } func1(x,y,z) { y=y+4; z=x+y+4; }plzz elaborate the answer explanation?thanks in advance.
0 votes
0 votes
0 answers
2
Rahul_Rathod_ asked Oct 3, 2018
522 views
from below list of parameter passing techniques, which parameter passing technique we can implement in c?1) call by value2) call by refference3) call by value result4) ca...
0 votes
0 votes
0 answers
3
sushmita asked Jan 18, 2017
867 views
foo(a, b, c) { b =b++; a = a++; c = a + b*10 } X=1; Y=2; Z=3; foo(X, Y+2, Z);CAN SOMEONE PLEASE TELL THE OUTPUTS FOR CALL BY REFERENCE AND CALL BY NAME. WHAT IS THE DIFER...
1 votes
1 votes
1 answer
4
set2018 asked Nov 4, 2017
346 views