scope - COP4020 Programming Languages Names, Scopes, and...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COP4020 Spring 2011 2 02/20/12 Overview Scope rules Static versus dynamic scoping Overloading
Background image of page 2
COP4020 Spring 2011 3 02/20/12 Scope Scope is the textual region of a program in which a name-to-object binding is active Statically scoped language : the scope of bindings is determined at compile time Used by almost all but a few programming languages More intuitive to user compared to dynamic scoping Dynamically scoped language : the scope of bindings is determined at run time Used in Lisp (early versions), APL, Snobol, and Perl (selectively)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COP4020 Spring 2011 4 02/20/12 Effect of Static Scoping The following pseudo-code program demonstrates the effect of scoping on variable bindings: a:integer procedure first a :=1 procedure second a:integer first() procedure main a :=2 second() write_integer( a ) a:integer main() a :=2 second() a:integer first() a :=1 write_integer( a ) Program execution: Program prints “1” binding
Background image of page 4
COP4020 Spring 2011 5 02/20/12 Effect of Dynamic Scoping The following pseudo-code program demonstrates the effect of scoping on variable bindings: a:integer procedure first a:=1 procedure second a:integer first() procedure main a :=2 second() write_integer( a ) a:integer main() a :=2 second() a:integer first() a :=1 write_integer( a ) Program execution: Program prints “2” binding Binding depends on execution
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COP4020 Spring 2011 6 02/20/12 Static Scoping The bindings between names and objects can be determined by examination of the program text Scope rules of a program language define the scope of variables and subroutines, which is the region of program text in which a name-to-object binding is usable Early Basic: all variables are global and visible everywhere Fortran 77: the scope of a local variable is limited to a subroutine; the scope of a global variable is the whole program text unless it is hidden by a local variable declaration with the same variable name Algol 60, Pascal, and Ada: these languages allow nested subroutines definitions and adopt the closest nested scope rule with slight variations in implementation
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/20/2012 for the course COP 4020 taught by Professor Engelen during the Spring '11 term at FSU.

Page1 / 19

scope - COP4020 Programming Languages Names, Scopes, and...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online