Lecture 3
Lecture 3
Scopes
• Introduction
• Names
• Variables
• The Concept of Binding
• Scope
• Scope and Lifetime
• Referencing Environments
• Named Constants
• Length
– If too short, they cannot be meaningful
– Language examples:
• FORTRAN 95: maximum of 31
• C99: no limit but only the first 63 are significant.
• C#, C++, Ada, and Java: no limit, and all are
significant
• Special characters
– PHP: all variable names must begin with dollar
signs
– Perl: all variable names begin with special
characters, which specify the variable’s type
– Ruby: variable names that begin with @ are
instance variables; those that begin with @@ are
class variables
• Case sensitivity
– Disadvantage: readability (names that look alike
are different)
• Names in the C-based languages are case sensitive
hence in C, names do not include upper case letters
• Worse in C++, Java, and C# because predefined
names are mixed case (e.g. parseInt)
• Special words
– An aid to readability; used to delimit or separate
statement clauses
• A keyword is a word that is special only in certain
contexts, e.g., in Fortran
– Real VarName (Real is a data type followed with a name,
therefore Real is a keyword)
– Real = 3.4 (Real is a variable)
– A reserved word is a special word that cannot
be used as a user-defined name
– Potential problem with reserved words: If there
are too many, many collisions occur (e.g.,
COBOL has 300 reserved words!)
• Type Inferencing
– Rather than by assignment statement, types are
determined (by the compiler) from the context
of the reference
• Storage Bindings & Lifetime
– Allocation - getting a cell from some pool of
available cells
– Deallocation - putting a cell back into the pool
• The lifetime of a variable is the time during
which it is bound to a particular memory
cell
...
call Sub2
…