ch5.2-scope
ch5.2-scope
Programming
Languages
Scope
ISBN 0-321-49362-1
Variable Attributes: Scope
Definitions
• The local variables of a program unit are those that
are declared in that unit
Scope could be
• Static Scope
• Block Structure
• Declaration Order
• Global scope
• Dynamic Scope
• Referencing environment
– Static scoping
•Reference to x in sub2 is to big’s x
• Block-structured language:
• Some languages allow new static scopes to be defined in
the midst of executable code.
• It allows a section of code its own local variables whose
scope is minimized.
• Such section of code is called a block
int i = 1;
int main() {
i=2;
{
int i=3;
cout << i;
for (i=1; i<=3; i++) {
cout << i;
}
cout << i;
}
cout << i;
for (i=3; i<=5; i++) {
cout << i;
}
cout << i;
}
1-12
Static Scope: The LET Construct…
• let construct in most functional languages
- Similar to the blocks of the imperative languages
• 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: Example:
(LET ( ( LET (
(name1 expression1) (top (+ab))
… (bottom (-cd))
(namen expressionn)) (/top bottom)
expression )
)
• In ML: let
let val top=a+b
val name1 = expression1 val bottom=c-d
… in
top/bottom
val namen = expressionn
end;
in
expression
end;
{ {
int x; …
… {
{ int x; // illegal
int x; // illegal …
… }
} …
… int x;
} }
{ {
int x; …
… {
{ int x; // legal
int x; // legal …
… }
} …
… int x;
} }
void fun() {
. . .
for (int count = 0; count < 10; count++){
. . .
}
. . .
}
int counter2=8;
int main() {
cout<<counter2<<endl;
cout<<staticCheck(5)<<endl;
cout<<staticCheck(15)<<endl;
cout<<counter2<<endl;
}
int counter2=8;
int main() {
cout<<counter2<<endl;
cout<<staticCheck(5)<<endl;
cout<<staticCheck(15)<<endl;
cout<<counter2<<endl;
}
Copyright © 2015 Pearson. All rights reserved. 1-23
Global Scope: C++ Global Variables
cout<<counter2<<endl; cout<<counter2<<endl;
cout<<staticCheck(5)<<endl; cout<<staticCheck(5)<<endl;
cout<<staticCheck(15)<<endl; cout<<staticCheck(15)<<endl;
cout<<counter2<<endl; cout<<counter2<<endl;
} }
Copyright © 2015 Pearson. All rights reserved. 1-24
Global Scope: C++ Function declaration/
definition
int counter2=8;
int staticCheck(int);
int main() {
cout<<counter2<<endl;
cout<<staticCheck(5)<<endl;
cout<<staticCheck(15)<<endl;
cout<<counter2<<endl;
}
• PHP
Variables are implicitly declared when they appear in a
statement
Global variable: any variable that is implicitly declared
outside any function
Local variables: variables implicitly declared in functions
Global variables are not implicitly seen in any function
Can be made visible in functions in two ways:
1) If a local variable has the same name as global one,
can access global variable via $GLOBALS array
2) If no local variable has the same name include the
global variable using global declaration
$day="Monday";
$month= "January";
function calendar()
{
$day = "Tuesday";
global $month;
print "local day is $day \n";
$gday= $GLOBALS['day'];
print "global day is $gday \n";
print "global month is $month ";
}
calendar();
• JavaScript:
• Global variables are similar to PHP
• But no way to access a global variable in a function that
has declared a local variable with the same name
• Python
– A global variable can be referenced in functions
– but can be assigned in a function only if it has been
declared to be global in the function
day= "Monday"
def calendar():
print ("The global day is:", day)
calendar()
day= "Monday"
def calendar(): day= "Monday"
print ("The global day is:", day) def calendar():
print ("The global day is:", day)
calendar() day="Tuesday"
print ("The global day is:", day)
calendar()
day= "Monday"
def calendar(): day= "Monday"
print ("The global day is:", day) def calendar():
print ("The global day is:", day)
calendar() day="Tuesday"
print ("The global day is:", day)
calendar()
– Static scoping
•Reference to x in sub2 is to big's x
– Static scoping
•Reference to x in sub2 is to big's x
– Dynamic scoping
•Reference to x in sub2 is to sub1's x
•
Copyright © 2015 Pearson. All rights reserved. 1-36
Scope Example
– Dynamic scoping
•Reference to x in sub2 is to big's x
int main() {
cout<<counter2<<endl;
cout<<staticCheck(5)<<endl;
cout<<staticCheck(15)<<endl;
cout<<counter2<<endl;
}
int main() {
cout<<counter2<<endl;
cout<<staticCheck(5)<<endl;
cout<<staticCheck(15)<<endl;
cout<<counter2<<endl;
}
Exercise:
What will be the final values of
variables x, y and a at the end of
execution of the main function
(before retrun) if it is a:
i) Static scoped language
ii) Dynamic scoped language?