Click here to load reader

Scope. 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

Embed Size (px)

Citation preview

  • Slide 1
  • Scope
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 5
  • Static Scope Example Procedure Big is x : Integer; procedure Sub1 is x : Integer; begin end; procedure Sub2 is begin x end; begin end;
  • Slide 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++; } }
  • Slide 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
  • Slide 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
  • Slide 9
  • PHP example $day = Monday; $month = February; function calendar() { $day = Wednesday; global $month; print local day is $day ; $gday = $GLOBALS[day]; print global day is $gday ; print global month is $month ; } calendar(); local day is Wednesday global day is Monday global month is January
  • Slide 10
  • Python Example day = Monday def tester(): print The global day is:, day tester(); The global day is: Monday
  • Slide 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
  • Slide 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
  • Slide 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
  • Slide 14
  • Dynamic Scope Scope based on calling sequence, not spatial relationship Only determined at run time Procedure Big is x : Integer; procedure Sub1 is x : Integer; begin end; procedure Sub2 is begin x end; begin end;
  • Slide 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
  • Slide 16
  • Scope and Lifetime Can appear related 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(); }
  • Slide 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
  • Slide 18
  • Ada Example 1 procedure Example is 2 A, B : Integer; 3... 4 procedure Sub1 is 5 X, Y : Integer; 6 begin 7... 8 end; 9 procedure Sub2 is 10 X : Integer; 11... 12 procedure Sub3 is 13 X: Integer; 14 begin 15... 16 end; 17 begin 18... 19 end; 20 begin 21... 22 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)
  • Slide 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
  • Slide 20
  • Dynamic scope example 1. void sub1(){ 2. int a, b; 3.... 4. } 5. void sub2(){ 6. int b, c; 7.... 8. sub1(); 9. } 10. void main(){ 11. int c, d; 12.... 13. sub2(); 14. } 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) Assume that the only functions calls are the following: main calls sub2, which calls sub1.
  • Slide 21
  • Named Constant A variable that is bound to a value only once Advantages: Readability e.g. PI instead of 3.141529 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