Chapter 5
Chapter 5
Chapter 5
Introduction
Imperative languages are abstractions of von Neumann architecture.
· Architecture components:
- Memory
• Stores both instructions and data
- Processor
• Provides operations for modifying the contents of the
memory
· The abstractions in a language for the memory cells of the machine are
variables.
· In some cases, the characteristics of the abstractions are very close to the
characteristics of the cells.
- integer variable represent in 4 bytes in Java
· In other cases, the abstractions are far removed from the organization of
the hardware memory,
- Three-dimensional array, which requires a software mapping
function to support the abstraction.
· Variables are characterized by attributes
- To design a type, must consider scope, lifetime, type checking,
initialization, and type compatibility
Names
Design issues for names:
o Are names case sensitive?
o Are special words reserved words or keywords?
A name is a string of characters used to identify some entity in a
program.
Names in most programming languages have the same form: a letter
followed by a string consisting of letters, digits, and underscore
characters ( _ ).
Names (continued)
Length
- If too short, they cannot be connotative
- Language examples:
• C99: no limit but only the first 63 are significant; also,
external names (i.e., outside functions) are limited to a
maximum of 31
• C# and Java: no limit, and all are significant
• C++: no limit.
Special characters
- PHP: all variable names must begin with dollar signs
- Perl: all variable names begin with special characters ($, @, or %),
which specify the variable's type
- Ruby: variable names that begin with @ are instance variables;
those that begin with ee are class variables
Case sensitivity
- Disadvantage: readability (names that look alike are different)
• E.g. int X, x;
• Names in the C-based languages are case sensitive
• Names in others are not
• Worse in C++, Java, and C# because predefined names are
mixed case (e.g. IndexOutOfBoundsException)
- Obviously, not everyone agrees that case sensitivity is bad for
names.
- In C, the problems of case sensitivity are avoided by the
convention that variable names do not include uppercase letters.
- In Java and C#, the problem cannot be escaped because many of
the predefined names include both uppercase and lowercase
letters.
• For example, the Java method for converting a string to an
integer value is parseInt, and spellings such as ParseInt and
parseint are not recognized.
- This is a problem of writability rather than readability, because the
need to remember specific case usage makes it more difficult to
write correct programs.
Names (continued)
Special words
o Special words in programming languages are used to make programs
more readable by naming actions to be performed.
o Special words also are used to separate the syntactic parts of
statements and programs.
o In most languages, special words are classified as reserved words,
which means they cannot be redefined by programmers.
o A reserved word is a special word that cannot be used as a user-
defined name.
o Potential problem with reserved words: If there are too many, many
collisions occur (e.g., COBOL has 300 reserved words!)
Variables
Variables Attributes
1) Name – we talk about this previously.
2) Address - the memory address with which it is associated
- A variable may have different addresses at different runs.
- A variable may have different addresses at different times during
execution
- A variable may have different addresses at different places in a
program.
• If a subprogram has a local variable that is allocated from
the run-time stack when the subprogram is called, different
calls may result in that variable having different addresses.
- If two variable names can be used to access the same memory
location, they are called aliases
- Aliases are created via pointers, reference variables, C and C++
unions
Address (continued)
o Aliases are created via array name in Java
o Aliases are harmful to readability (program readers must
remember all of them)
3) Type - determines the range of values of variables and the set of
operations that are defined for values of that type; in the case of floating
point, type also determines the precision.
o For example, the int type in Java specifies a value range of
-2,147,483,648 to +2,147,483,647 and arithmetic operations for
addition, subtraction, multiplication, division, and modulus.
4) Value – the contents of the location with which the variable is
associated
- The l-value of a variable is its address
- The r-value of a variable is its value
· Abstract memory cell – the physical cell or collection of cells associated
with a variable
Explicit/Implicit Declaration
C# - a variable can be declared with var and an initial value. The initial
value sets the type
var sum = 0;
The types of sum, total, and name are int, float, and string, respectively.
Visual Basic 9.0+, ML, Haskell, and F# use type Inferencing. The
context of the appearance of a variable determines its type
list = 17.3;
1- Static Scope
function big() {
function subl () {
var x = 7;
sub2 () ;
}
function sub2 () {
var y = x;
}
x = 3;
subl ();
var
o Under static scoping, the reference to the variable x in sub2 is to
the x declared in the procedure big.
o The x declared in sub1 is ignored, because it is not in the static
ancestry of sub2.
2- Blocks
void sub() {
int count;
while (...) {
legal in C and C++
int count;
but not in Java and C#
count++;
...
}
…
}
· Most functional languages include some form of let construct
· A let construct has two parts
- The first part binds names to values
- The second part uses the names defined in the first part
· In Scheme:
(LET (
(name, expression,)
(name, expression,) )
· Consider the following call to LET:
(LET (
(top (+ a b))
(bottom (- c d) ))
(/ top bottom)
- This call computes and returns the value of the expression (a + b) / (c - d)
In ML:
let Let
val name, = expression1
Val top = a + b
val name_n = expression_n
Val bottom = c – d
in
In
expression
Top/bottom
end;
End;
In F#:
- First part: let left_side = expression
- (left_side is either a name or a tuple pattern)
- All that follows is the second part
Consider the following code:
let n1 =
let n2 = 7
let n3 n2 + 3
n3;;
let n4 = n3 + n1;;
- The scope of n1 extends over all of the code.
- However, the scope of n2 and n3 ends when the indentation ends. So, the
use of n3 in the last let causes an error.
- The last line of the let n1 scope is the value bound to n1; it could be any
expression.
3- Declaration Order
C99, C++, Java, and C# allow variable declarations to appear anywhere a
statement can appear
- In C99, C++, and Java, the scope of all local variables is from the
declaration to the end of the block
- In C#, the scope of any variable declared in a block is the whole block,
regardless of the position of the declaration in the block
o However, a variable still must be declared before it can be used
Example of C#:
C# does not allow the declaration of a variable in a nested block to have
the same name as a variable in a nesting scope.
{ int x; {
} }
} int x;
void fun () {
day
tester ()
Dynamic Scope
The scope of variables in APL, SNOBOL4, and the early versions of Lisp
is dynamic.
Dynamic scoping is based on calling sequences of program units, not their
textual layout (temporal versus spatial)
The scope can be determined only at run time.
References to variables are connected to declarations by searching back
through the chain of subprogram calls that forced execution to this point
Scope Example
function big () {
function sub1 () Big calls Sub1
var x = 7;
Sub1 calls sub2
function sub2 () {
Sub2 uses x
var y = x;
}
var x = 3;
}
- Static scoping
Reference to x in sub2 is to big's x
- Dynamic scoping
Reference to x in sub2 is to subl's x
It may reference the variable from either declaration of x,
depending on the calling sequence.
Scope and lifetime are sometimes closely related, but are different
concepts
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.
Consider a static Variable in a C or C++ function
Referencing Environments
Named Constants