CS 342 Lecture -*- Outline -*- * Scope rules ** Static (lexical) scoping procedures called in environment of def. -------------- begin boolean debugging; procedure debug_print_real(x); value x; real x; begin if debugging then comment ...; end; procedure f(a,b,c); real a,b,c; begin debug_print_real(a); comment ...; end; procedure p; begin boolean debugging; debugging := true; f(x,y,z) end; comment ** main program **; begin boolean debugging; debugging := false; comment ...; p; end end ------------- (draw contour diagrams for static and dynamic scope while P is running) ** Dynamic scoping (fluid binding) procedures called in environment of caller. ** Analysis *** Benefits of dynamic scoping -Can override bindings dynamically (even accidentally) -textually unrelated parts of program can communicate without using globals or parameters -possible to access parts of envrionment that cannot be passed as parameters (procedures in some languages) *** Costs of dynamic scoping (see above) -more expensive to implement at run-time programs are harder to read, because can't tell what names mean -static structure of program does not relfect dynamic structure violation of structure principle -cannot do static type checking (in general) x := 3, how do we know that x has type integer?