main() { /** C **/
int a = 5;
printf("%d\n", x);
{
int x = 10;
int y = 20;
printf("%d\n", x);
{
int x = 15;
int z = 20;
printf("%d\n", x);
}
}
}
Local variable – visible only to
the block where variable being
declared
Non-local variable – visible to all
segments of the code
Static Scope
Static Scope
2 categories – nested static scope and cannot be nested
JavaScript and PHP do not support nested static scopes
But JavaScript and PHP do support function-level scope
var abc = false;
if ( s == "HELLO" ) {
abc = true;
var abc = 500;
document.write("abc is " + abc)
}
document.write("abc is still " + abc)
Example :
main() { /** C **/
int x = 5;
printf("%d\n", x);
{
int y = 20;
printf("%d\n", x);
{
int x = 15;
int z = 20;
printf("%d\n", x);
}
}
}
Declaration of x will be searched
locally first, then from the calling
procedure
x is declared locally.
Therefore x=5 is hidden.
Blocks
A method of creating static scopes inside program units --
from ALGOL 60
allows a section of code to have its own local variables whose scope is
minimized.
Examples:
C and C++: for (...) {
int index;
...
}
Ada: declare LCL : FLOAT;
begin
...
end
compound statement
– define new scope
declare statement –
specified block
This is called as BLOCK
Example:
Error!!
Evaluation of Static Scoping
Assume MAIN calls A and B
A calls C and D
B calls A and E
MAINMAIN
E
A
C
D
B
A B
C D E
MAIN is Static
parent of B
MAIN and A
are static
ancestry of
C and D
Static Scope Example
MAIN MAIN
A B
C D E
A
C
B
ED
Possible procedure callsDesired procedure calls
Static Scope (continued)
Suppose the spec is changed so that D must now
access some data in B
Solutions:
Put D in B (but then C can no longer call it and D cannot
access A's variables)
Move the data from B that D needs to MAIN (but then all
procedures can access them)
Same problem for procedure access
Overall: static scoping often encourages many
globals
Example : Static Scope
Answer : main Answer : epsilon
Answer : all procedures
Dynamic Scope
Scope Example
MAIN
- declaration of x
SUB1
- declaration of x -
...
call SUB2
...
SUB2
...
- reference to x -
...
...
call SUB1
…
MAIN calls SUB1
SUB1 calls SUB2
SUB2 uses x
x is referenced from the
calling procedure which is
SUB1
Scope Example
Static scoping
Reference to x is to MAIN's x
Dynamic scoping
Reference to x is to SUB1's x
Evaluation of Dynamic Scoping:
Advantage: convenience
Disadvantage: poor readability
Example 1: Dynamic Scope
Answer: Kappa
Example 2: Dynamic Scope
Answer: iota & lambda
begin
integer m, n;
procedure hardy;
begin
print("in hardy -- n = ", n);
end;
procedure laurel(n: integer);
begin
print("in laurel -- m = ", m);
print("in laurel -- n = ", n);
hardy;
end;
m := 50;
n := 100;
print("in main program -- n = ", n);
laurel(1);
hardy;
end;
Static scoping:
in main program -- n =
in laurel -- m =
in laurel -- n =
in hardy -- n =
in hardy -- n =
Dynamic scoping:
in main program -- n =
in laurel -- m =
in laurel -- n =
in hardy -- n =
in hardy -- n =
100
50
1
100
100
100
50
1
1
100
#include<stdio.h>
int x = 10;
int f()
{
return x;
}
int g()
{
int x = 20;
return f();
}
int main()
{
printf("%d", g());
return 0;
}
Static scoping:
Value printed in main is
in static scoping the compiler first searches
in the current block, then in the surrounding
blocks successively and finally in the global
variables
Dynamic scoping:
Value printed in main is
in dynamic scoping the compiler first
searches the current block and then successively
all the calling functions.
10
20
Scope and Lifetime
Scope and lifetime are sometimes closely
related, but are different concepts
Consider a static variable in a C or C++
function
Statically bound to the scope of the function and
to storage.
But lifetime extends over the entire execution of
program of which it is a part.
Example
Scope of variable sum
contained in function
compute only
Lifetime of variable sum
begins when function
compute executes and
extends until function
printheader
complete