|
|
Scope in Programming Languages Amruth N. Kumar Eric Fernandes, Steve Obringer Ramapo College of New Jersey Introduction Scope is an important concept in
programming languages – one cannot read or write large programs without
properly understanding the concept of scope. The scope of a variable in a program is the lines of code
in the program where the variable can be accessed. Historically, two different types of scope have been used in programming languages: static scope and dynamic scope. Most modern programming languages such as Pascal, C++ and Java use static scope. Some languages such as APL, Snobol4 and early versions of LISP use dynamic scope. As students of Programming Languages, we will study both these types. The converse of scope is
referencing environment. The
referencing environment of a procedure is the set of all the variables
whose scope extends to the procedure. It may be divided into local and
non-local environments. The local referencing environment of a
procedure consists of all the variables declared within the procedure, and
includes any formal parameters of the procedure. The non-local referencing
environment consists of all the variables not declared in the
procedure, but whose scope extends to the procedure. The concept of scope applies not only to variable
names but also to the names of procedures. The
scope of the name of a procedure determines all the procedures/functions in a
program that can call the procedure. Scope Courseware
This courseware addresses all these aspects of scope. It comprises of five modules addressing the following topics: 1. Static Scope of variables 2. Static Referencing Environment of a procedure 3. Dynamic Scope of variables 4. Dynamic Referencing Environment of a procedure 5. Callability of procedures - Scope of procedure names Pascal is popularly used to illustrate the intricacies of static scope since it allows procedure definitions to be nested. In order to help you compare and contrast static scope with dynamic scope, and to reduce the burden of having to learn new syntax, this courseware will use Pascal-like programs to illustrate dynamic scope also.
Contents of Each
Module Each of the five modules contains a tutorial
and an applet. The applet is hyperlinked from the tutorial. The information
in each tutorial is organized under the following titles: Definition: In this section, the definition of the concept is presented.
Where applicable, the definition is clarified in the context of Pascal. Example: This section includes the following items: 1. A sample program, and a problem based on the program; 2. Answer to the problem, along with explanation. Pragmatics: In this section, additional questions are presented based on the
earlier example. These questions are designed to help you consider other
aspects of the problem, and extend what you have learned to other concepts. Algorithms to
determine “Scope”: Whereas the definition section
describes the relevant “theory”, this section lists the algorithms that you
may want to use to apply the theory to solve problems. Test your
understanding - Instructions: This section lists the following items: 1. Your options for solving problems using the applet; 2. The steps you must follow to solve each problem; 3. A screen shot illustrating these steps; 4. A hyperlink to the applet itself. Note that when you click on the hyperlink, the following two windows will be launched: a. A web
page to record how many problems you have attempted and how many problems you
have solved correctly. b. The
applet window that you will use to solve the problems. You may solve as many
problems as you wish without exiting the applet window. Please pay
particular attention to the feedback provided by the applet for each problem. Once you exit the applet, you can
re-launch it by clicking on the “Create New Problem” button on the
accompanying web page. Acknowledgements Partial
support for this work was provided by the National Science Foundation’s
Course, Curriculum and Laboratory Improvement Program under grant
DUE-0088864, January 2001-December 2002. |