LOCAL REFERENCING ENVIRONMENTS
• Subprograms are generally allowed to define their own variables, thereby defining local referencing
environments. Variables that are defined inside subprograms are called local variables because access to
them is usually restricted to the subprogram in which they are defined.
• If local variables are stack dynamic, they are bound to storage when the subprogram begins execution
and unbound from storage when that execution terminates. An advantage of this is flexibility.
• It is important that recursive subprograms have stack dynamic local variables.
• Another advantage is that some of the storage for local variables of all subprograms can be shared
• Main disadvantages of stack dynamic local variables are:
o Cost of time required to allocate, initialize and de-allocate for each activation
o Accesses of stack dynamic local variables must be indirect, where accesses to static can be
direct
o Stack dynamic local variables, the subprograms cannot retain data values of local variables
between calls.
• The primary advantage of static local variables is that they are very efficient because of no indirection.
Static and Dynamic Scoping
The scope of a variable x in the region of the program in which the use of x refers to its declaration. One of the basic
reasons for scoping is to keep variables in different parts of the program distinct from one another. Since there are only a
small number of short variable names, and programmers share habits about naming of variables (e.g., I for an array
index), in any program of moderate size the same variable name will be used in multiple different scopes.
Scoping is generally divided into two classes:
1. Static Scoping
2. Dynamic Scoping
Static Scoping:
Static scoping is also called lexical scoping. In this scoping, a variable always refers to its top-level environment. This is
a property of the program text and is unrelated to the run-time call stack. Static scoping also makes it much easier to
make a modular code as a programmer can figure out the scope just by looking at the code. In contrast, dynamic scope
requires the programmer to anticipate all possible dynamic contexts.
In most programming languages including C, C++, and Java, variables are always statically (or lexically) scoped i.e.,
binding of a variable can be determined by program text and is independent of the run -time function call stack.
For example, the output for the below program is 10, i.e., the value returned by f() is not dependent on who is calling it
(Like g() calls it and has a x with value 20). f() always returns the value of global variable x.
To sum up, in static scoping the compiler first searches in the current block, then in global variables, then in successively
smaller scopes.
Dynamic Scoping:
With dynamic scope, a global identifier refers to the identifier associated with the most recent environment and is
• Subprograms are generally allowed to define their own variables, thereby defining local referencing
environments. Variables that are defined inside subprograms are called local variables because access to
them is usually restricted to the subprogram in which they are defined.
• If local variables are stack dynamic, they are bound to storage when the subprogram begins execution
and unbound from storage when that execution terminates. An advantage of this is flexibility.
• It is important that recursive subprograms have stack dynamic local variables.
• Another advantage is that some of the storage for local variables of all subprograms can be shared
• Main disadvantages of stack dynamic local variables are:
o Cost of time required to allocate, initialize and de-allocate for each activation
o Accesses of stack dynamic local variables must be indirect, where accesses to static can be
direct
o Stack dynamic local variables, the subprograms cannot retain data values of local variables
between calls.
• The primary advantage of static local variables is that they are very efficient because of no indirection.
Static and Dynamic Scoping
The scope of a variable x in the region of the program in which the use of x refers to its declaration. One of the basic
reasons for scoping is to keep variables in different parts of the program distinct from one another. Since there are only a
small number of short variable names, and programmers share habits about naming of variables (e.g., I for an array
index), in any program of moderate size the same variable name will be used in multiple different scopes.
Scoping is generally divided into two classes:
1. Static Scoping
2. Dynamic Scoping
Static Scoping:
Static scoping is also called lexical scoping. In this scoping, a variable always refers to its top-level environment. This is
a property of the program text and is unrelated to the run-time call stack. Static scoping also makes it much easier to
make a modular code as a programmer can figure out the scope just by looking at the code. In contrast, dynamic scope
requires the programmer to anticipate all possible dynamic contexts.
In most programming languages including C, C++, and Java, variables are always statically (or lexically) scoped i.e.,
binding of a variable can be determined by program text and is independent of the run -time function call stack.
For example, the output for the below program is 10, i.e., the value returned by f() is not dependent on who is calling it
(Like g() calls it and has a x with value 20). f() always returns the value of global variable x.
To sum up, in static scoping the compiler first searches in the current block, then in global variables, then in successively
smaller scopes.
Dynamic Scoping:
With dynamic scope, a global identifier refers to the identifier associated with the most recent environment and is