retagged by
4,177 views
22 votes
22 votes

For the following code, indicate the output if 

  1. static scope rules
  2. dynamic scope rules

are used

var a,b : integer;

procedure P;
    a := 5;
    b := 10;
end {P};

procedure Q;
    var a, b : integer;
    P;
end {Q};

begin 
    a := 1;
    b := 2;
    Q;
    Write ('a = ', a, 'b = ', b);
end
retagged by

3 Answers

Best answer
26 votes
26 votes

In static scoping, if a variable is not found (variable definition - memory allocation) in the local scope (current function, which includes the current block, then parent block etc.), it is looked upon in global scope. In dynamic scoping, if a variable is not found in local scope, it is looked upon in the function which called the current executing one.

  1. $a = 5, b = 10$. main is using global variables. P is also using global variables.
  2. $a = 1, b = 2$. main is using global variables. P is using the local variables defined in Q.


(The modification in Q, happens to the variables in P but in main we use the global variables)

edited by
14 votes
14 votes

Using Static scope rule output: 5,10

Using Dynamic scope rule output :1,2

Related questions

24 votes
24 votes
1 answer
4
Kathleen asked Sep 12, 2014
5,737 views
Match the pairs in the following questions by writing the corresponding letters only.$$\begin{array}{|ll|ll|}\hline \text{(a)} & \text{Buddy system} & \text{(p)} & \tex...