You are on page 1of 11

Scope Vs.

Lifetime

• Scope defines the region of the program text over which


the variable is visible.

• Lifetime is the period of execution.

Example:
● Global variable
scope: complete program
lifetime: execution time of the program

● Local variable in a function definition


scope: function definition block
lifetime: each invocation of the function

BITS Pilani, Hyderabad


Pilani Campus
Campus
Scope

• Scope is the range of statements in which the variable is visible.


• A variable is visible in a statement if it can be referenced in that
statement.
• Scope rules of a language determine how references to variables
declared outside the currently executing subprogram or block are
associated with their declarations.
• Binding occurrence: Declaration of a variable, which declaration a
variable is referring to. (binding of a variable with its type)
• Bound occurrence: Usage of a variable, where the variable is used.

BITS Pilani, Hyderabad


Pilani Campus
Campus
Static scoping

• Method of binding for non-local variables.


• Scope of a variable can be determined statically (at
compile-time).
• Types of static scoped languages: those in which
subprograms can be nested (our focus) and those in
which subprograms cannot be nested.
• Static scoping is used when declaration can be mapped
to the use of a variable at compile time only. Example, C,
Pascal.
• This type is based on the spatial relationship between
the subprograms.

BITS Pilani, Hyderabad


Pilani Campus
Campus
Example
function big()
{
function sub1()
{
var x=7;
sub2();
}
function sub2()
{
var y=x;
}
var x=3;
sub1();
}

• Under static scoping, the reference to the variable x in sub2 is to the x declared in the procedure
big because big is the static parent of sub2. Since no declarations for x is found in the subprogram
sub2 where the reference (use) of x occurs, the search continues in the static parent of sub2
which is big.
• The x declared in sub1 is ignored because it is not in the static ancestor list of sub2.
• If the variables is declared within the subprogram in which it is referenced, then there is no need
of any application of static scoping rules as the local declaration will be used.

BITS Pilani, Hyderabad


Pilani Campus
Campus
Dynamic Scoping

• Dynamic scoping is used when binding of the use of a


variable to its declaration is done at run-time. This is
done based upon the calling sequence of subprograms,
not on their spatial relationship to each other.
• If the variables is declared within the subprogram in
which it is referenced, then there is no need of any
application of dynamic scoping rules as the local
declaration will be used.

BITS Pilani, Hyderabad


Pilani Campus
Campus
Example
function big()
{
function sub1()
{
var x=7;
}
function sub2()
{
var y=x;
var z=3;
}
var x=3;
}
• Example: big calls sub1 and sub1 calls sub2:
• Under dynamic scoping, the reference to the variable x in sub2 is to the x declared in the
procedure sub1 because sub1 is the dynamic parent of sub2. Since no declarations for x is found
in the subprogram sub2 where the reference (use) of x occurs, the search continues in the
dynamic parent of sub2 which is sub1. Dynamic ancestor list of sub2 will contain sub1 and then
big starting from sub1.
• Example: sub2 is called directly from big:
• Under dynamic scoping, the reference to the variable x in sub2 is to the x declared in the
procedure big because big is the dynamic parent of sub2.

BITS Pilani, Hyderabad


Pilani Campus
Campus
Example: Static & Dynamic
Scoping
P()
{
int x;
x=1;
Q()
{ Output:
x=x+5;
Static scoping: 6 11 17
printf(“%d”, x);
Dynamic scoping: 6 17 22
}
R()
{
int x;
x=2;
x=x+10;
Q();
x=x+5;
printf(“%d”, x);
}
Q();
R();
} BITS Pilani, Hyderabad
Pilani Campus
Campus
Solution

BITS Pilani, Hyderabad Campus


Example: Visibility
main()
{
int x=0,y=0,z=0;
Assume static scoping:
sub1() What is the visibility of variables
{ in each function?
int a=0, y=2, z=3;
sub2() Answer:
{
sub1: a from sub1, y from sub1, z
int a=2, b=2, z=5;
}
from sub1, x from main
} sub2: a from sub2, b from sub2, z
sub3() from sub2, y from sub1, x from
{ main
int a=2, x=5, w=10;
} sub3: a from sub3, x from sub3, w
} from sub3, y from main, z from
main

BITS Pilani, Hyderabad


Pilani Campus
Campus
Example (Visibility)
void fun1(void);
void fun2(void); Assume dynamic scoping:
void fun3(void);
void main() main calls fun3, fun3 calls fun2,
{ fun2 calls fun1.
int a, b, c;
}
void fun1(void) What is the visibility of variables in
{
int b,c,d;
the last call ?
}
void fun2(void)
Answer:
{
int c, d, e; b,c,d from fun1, e from fun2, f from fun3 and a
} from main
void fun3(void)
{
int d, e, f;
}
BITS Pilani, Hyderabad
Pilani Campus
Campus
Evaluation of static & dynamic
scoping
Static scoping:
• Restructuring of the code can change the access restrictions and hence the
results obtained by static scoping .

Dynamic scoping:
• The local variables of a subprogram are visible to any other executing
subprogram which is called thus the programs are less reliable.
• In dynamic scoping, type checking of non-local variables cannot be done
statically as their types are not known at compile time (depends upon the
calling sequence of subprograms).
• Accesses to non-local variables in dynamic-scoped languages takes longer
than in static scoping.
Static scoping is more widely used as programs are more reliable and faster to
execute.

BITS Pilani, Hyderabad


Pilani Campus
Campus

You might also like