Download presentation
1
Scope
2
Scope of a variable 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 A variable is local in a program unit (block) if it is declared there A variable is nonlocal if it is visible, but not declared in the program unit Scope rules of a language determine how references to names are associated with variables
3
Static Scope One method of binding names to nonlocal variables
Determination of the scope of a variable can be accomplished prior to execution Search process: search declarations, first locally, then in increasingly larger enclosing scopes, until one is found for the given name Enclosing static scopes (to a specific scope) are called its static ancestors; the nearest static ancestor is called a static parent
4
Static Scope continued
Variables can be hidden from a unit by having a "closer" variable with the same name C++ and Ada allow access to these "hidden" variables In Ada: unit.name In C++: class_name::name
5
Static Scope Example Procedure Big is x : Integer; procedure Sub1 is begin … end; procedure Sub2 is … x …
6
Blocks Allow new static scope to be defined in the midst of executable code Allows for local variables with minimized scope { } of C-based languages void sub () { int count; … while ( … ) { int count = 0; count++; }
7
Declaration Order In some languages all data declarations in a function, except those in nested blocks, must appear at the beginning of the function C++ and Java, a declaration can appear anywhere a statement can appear Resulting scope is not strictly a compound statement or subprogram, since it begins at the declaration and ends at the end of the block in which it appears
8
Global Scope Definitions outside functions in a file create global variables Definitions and declarations Declarations specify types and other attributes, but do not cause allocation of storage Definitions specify attributes and cause allocation of storage Languages implement global scope in a variety of ways
9
PHP example local day is Wednesday global day is Monday global month is January $day = “Monday”; $month = “February”; function calendar() { $day = “Wednesday”; global $month; print “local day is $day <br />”; $gday = $GLOBALS[‘day’]; print “global day is $gday <br />”; print “global month is $month <br />”; } calendar();
10
Python Example day = “Monday” def tester(): print “The global day is:”, day tester(); The global day is: Monday
11
Python example (2) day = “Monday” def tester(): print “The global day is:”, day day = “Tuesday” print = “The new value of day is:”, day tester() UnboundLocalError
12
Python example (3) day = “Monday” def tester(): global day print “The global day is:”, day day = “Tuesday” print “The new value of day is :”, day tester() The global day is: Monday The new value of day is: Tuesday
13
Problems with Static Scope
In many cases, allows more access to variables and subprograms than is necessary Program evolution may cause issues Restructuring changes Encourages global variables
14
Dynamic Scope Scope based on calling sequence, not spatial relationship Only determined at run time Procedure Big is x : Integer; procedure Sub1 is begin … end; procedure Sub2 is … x …
15
The trouble with dynamic scope
All local variables of a subprogram are visible to any other executing subprogram Not possible to statically type check references to non-locals Decreased readability Efficiency concerns
16
Scope and Lifetime Can appear related … but are actually unrelated
e.g. variable defined in a Java method that calls no other methods … but are actually unrelated e.g. use of static in C and C++ e.g. when subprograms are involved void printheader(){ … } void compute() { int sum; printheader();
17
Referencing Environment
The collection of all names that are visible in the statement In a static-scoped language, it is the local variables plus all of the visible variables in all of the enclosing scopes
18
Ada Example 1 procedure Example is 2 A, B : Integer; procedure Sub1 is 5 X, Y : Integer; 6 begin end; 9 procedure Sub2 is 9 procedure Sub2 is 10 X : Integer; procedure Sub3 is 13 X: Integer; 14 begin end; 17 begin end; 20 begin end; “line” (Referencing Environment) “7” (X, Y of Sub1, A,B of Example) “15” (X of Sub3, A,B of Example) “18” (X of Sub2, A,B of Example)
19
Dynamic Referencing Environment
In a dynamically-scoped language, it is the local variables plus all visible variables in all active subprograms A subprogram is active if its execution has begun but has not yet terminated
20
Dynamic scope example void sub1(){ int a, b; ... } void sub2(){
int b, c; sub1(); void main(){ int c, d; sub2(); Assume that the only functions calls are the following: main calls sub2, which calls sub1. “line” (Referencing Environment) “3” (a, b of sub1, c of sub2, d of main) “7” (b, c of sub2, d of main) “12” (c, d of main)
21
Named Constant A variable that is bound to a value only once
Advantages: Readability e.g. PI instead of Program Reliability/Modifiability e.g. arrays and loops Binding of constants can be either static or dynamic e.g. Fortran95 requires that a named constant be initialized with a constant expression aka manifest constant e.g. Ada, C++ allow dynamic bindings of values to constants
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.