Lec 06
Lec 06
• Introduction
• Names
• Variables
• The Concept of Binding
• Scope
• Scope and Lifetime
• Referencing Environments
• Named Constants
• Length
- Mostly format: a letter followed by a string consists
of letters, digits, and underscore characters ( _ )
– If too short, they cannot be connotative
– Language examples:
• C# and Java: no limit, and all are significant
• C++: no limit, but implementers often impose one
• Special characters
– PHP: all variable names must begin with dollar
signs ($)
• Case sensitivity
– Disadvantage: readability (names that look alike
are different)
• 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)
1-13
Static and Dynamic Binding
1-20
Storage Binding
• 4 categories: static, stack-dynamic, explicit heap-
dynamic, and implicit heap-dynamic.
void fun() {
. . .
for (int count = 0; count < 10; count++){
. . .
}
.
}
• PHP
– Programs are embedded in HTML markup
documents, in any number of fragments, some
statements and some function definitions
– The scope of a variable (implicitly) declared in a
function is local to the function
– The scope of a variable implicitly declared
outside functions is from the declaration to the
end of the program, but skips over any
intervening functions
• Global variables can be accessed in a function
through the $GLOBALS array or by declaring it global
Copyright © 2015 Pearson. All rights reserved. 1-30
Global Scope (continued)
$day = "Monday";
$month = "January";
function calendar() {
$day = "Tuesday";
global $month;
print "local day is $day <br />";
$gday = $GLOBALS['day'];
print "global day is $gday <br \>";
print "global month is $month <br />";
}
calendar();
– Dynamic scoping
• Reference to x in sub2 is to sub1's x = 7
1) Search for x begins in sub2, but no declaration for x is found.
2) Search dynamic parent / calling function of sub2, sub1 x is found.
void sub1() {
int a, b;
. . . 1
} /* end of sub1 */
void sub2() {
int b, c;
.. . . 2
sub1();
} /* end of sub2 */
void main() {
int c, d;
. . . 3
sub2();
} /* end of main */
• C++
const int result = 2 * width + 1;