Professional Documents
Culture Documents
Scope vs. Lifetime: Scope Defines The Region of The Program Text Over Which The Variable Is Visible
Scope vs. Lifetime: Scope Defines The Region of The Program Text Over Which The Variable Is Visible
Lifetime
Example:
● Global variable
scope: complete program
lifetime: execution time of the program
• 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.
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.