edited by
9,199 views
40 votes
40 votes

The following program fragment is written in a programming language that allows global variables and does not allow nested declarations of functions.

global int i=100, j=5;
void P(x) {
	int i=10;
    print(x+10);
    i=200;
    j=20;
    print (x);
}
main() {P(i+j);}

If the programming language uses static scoping and call by need parameter passing mechanism, the values printed by the above program are:

  1. $115, 220$
  2. $25, 220$
  3. $25, 15$
  4. $115, 105$
edited by

5 Answers

0 votes
0 votes

The best ans has already cleared the doubt between call by name and call by need. But i think some more points about static and dynamic scoping will provide a clear picture.

Static Scoping : More dominant than dynamic scoping. Means all languages generally prefer Static Scoping ( except language Perl which supports both).

Compiler in Static Scoping searches first in the current block and then globally. 

Dynamic Scoping: Identifier has a global stack of bindings and the occurrence of an identifier is searched in the most recent binding. 

In simpler terms, in dynamic scoping, the compiler first searches the current block and then successively all the calling functions.

Dynamic Scoping doesn't depend on how the code is written but on how the code executes.

Each time a new function is executed, a new scope is pushed onto the stack.

Hope it helps 🙂

Answer:

Related questions

22 votes
22 votes
3 answers
4