0% found this document useful (0 votes)
13 views14 pages

CSCI351-Week 7-Lecture 1

Uploaded by

AbuSaMrA /Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views14 pages

CSCI351-Week 7-Lecture 1

Uploaded by

AbuSaMrA /Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Chapter 5: Names, Bindings,

and Scopes
Week 7 Lecture 1 (3/4)
Objectives
5.6 Scope and Lifetime

5.7 Referencing Environments

5.8 Named Constants

CSCI351 - Concepts of Programming


1-2
Languages
Scope and Lifetime (3/2)
• The scope and lifetime of a variable appear to be related.

• For example, consider a variable that is declared in a Java


method that contains no method calls.
– The scope of such a variable is from its declaration to the end of
the method.

– The lifetime of that variable is the period of time beginning when


the method is entered and ending when execution of the method
terminates.

• Scope and lifetime are also unrelated when subprogram calls


are involved.

CSCI351 - Concepts of Programming


1-3
Languages
Scope and Lifetime (4/2)
• Consider the following C++ functions:
– The scope of the variable sum is
completely contained within the void printheader() {
. . .
function compute only.
} // end method
– The lifetime of sum extends over the
void compute() {
time during which printheader int sum;
. . .
executes. printheader();
} // end method
– Whatever storage location sum is
bound to before the call to printheader,
that binding will continue during and
after the execution of printheader.

CSCI351 - Concepts of Programming


1-4
Languages
Referencing Environments (5/5)
• The referencing environment of a statement is the collection
of all variables that are visible in the statement.

• In a static-scoped language, it is the local variables plus all


the visible variables of its ancestors.

• In a dynamic-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.

• In Python, scopes can be created by function definitions (new


environment for each scope created)
CSCI351 - Concepts of Programming
1-5
Languages
Referencing Environments (6/5)
• Consider the following Python skeletal program:

CSCI351 - Concepts of Programming


1-6
Languages
Referencing Environments (7/5)
• The referencing environments of the indicated program
points are as follows:

CSCI351 - Concepts of Programming


1-7
Languages
Referencing Environments (8/5)
• Consider the following program. Assume that the only function
calls are the following: main calls sub2, which calls sub1

Dynamically scoped
language

CSCI351 - Concepts of Programming


1-8
Languages
Referencing Environments (9/5)
• The referencing environments of the indicated program
points are as follows:

CSCI351 - Concepts of Programming


1-9
Languages
Named Constants
• A named constant is a variable that is bound to a value
only once.

• Named constants are useful as aids to readability and


program reliability.

• Readability can be improved, for example, by using the


name PI instead of the constant 3.14159265.

• Another important use of named constants is to


parameterize a program.

CSCI351 - Concepts of Programming


1-10
Languages
Named Constants
• Consider the following skeletal Java program segment:

CSCI351 - Concepts of Programming


1-11
Languages
Named Constants
• Dynamic binding of values to named constants for other
languages:
– FORTRAN 90: real, parameter :: pi = 3.1415927;

– Ada: Byte_Per_Page : constant := 512;

– C++: const int result = 2 * width + 1;

– Java: final double PI = 3.1415;


– C#: const (static) and readonly (dynamic)

– Python: Const_Name = “Python”;

CSCI351 - Concepts of Programming


1-12
Languages
Variable Initialization
• The binding of a variable to a value at the time it is
bound to storage is called initialization

• Initialization is often done on the declaration statement,


for example in Java:
int sum = 0;

String message = “Java”;

char letter = „u‟;

CSCI351 - Concepts of Programming


1-13
Languages
CSCI351 - Concepts of Programming
1-14
Languages

You might also like