in Programming in C
1,831 views
4 votes
4 votes
Indicate results of the following program if the language uses
i)Static scope rule and
ii) Dynamic scope rules (GATE-1989)
var x,y:integer;
procedure A(var z:integer);
var x:integer;
begin
x:=1;
B;
z:=x;
end;
procedure B;
begin
x:=x+1;
end;
begin
x:=5;
A(y);
write(y)
end;
in Programming in C
1.8k views

4 Answers

3 votes
3 votes
If call by value is used. It must be 0 in both the cases.

Since, Global variables are initialized with 0 and since we are not changing value of y anywhere in the program, it will remain same.

1 comment

edited by

For static case : 'y' should be 1 as it is call by reference method here.

0
0
3 votes
3 votes

In static scoping value will be 6,And in Dynamic scoping value will be 2

3 Comments

Value of y is printed which must be 1 for static scoping and 2 for dynamic scoping (assuming pass by reference)

1
1
x value is not printed anywhere in the given program. Only y value is printed .
1
1
Can you explain in detail please?
0
0
0 votes
0 votes
static=6

dynamic=2
0 votes
0 votes

In static scoping: 1

In dynamic scoping: 2

if call by reference is used then in static scoping when the function B is called inside the function A 

the statement x= x+1 will change the global value of x = 5  6

but the main function is never using this global value of x it is printing the value of actual parameter y which is the formal parameter z in function A.

and z = 1, so, 1 will be printed in the case of static scope.

and when Dynamic scoping  is used and the function  B is called inside function A 

the statement x= x+1 will change the value of the variable x present in the calling function i.e. A, so X= 1 2

Related questions

7 votes
7 votes
3 answers
4