SlideShare a Scribd company logo
Chapter 5: Names, Bindings, and Scopes 
Principles of Programming Languages
Contents 
•Names 
•Variables 
•The Concept of Binding 
•Scope and Lifetime 
•Referencing Environments 
•Named Constants
Introduction 
•Imperative languages are abstractions of von Neumann architecture 
–Memory  variables 
•Variables characterized by attributes, among of them is type 
•Scope and lifetime of variables are important issues when designing type
Variable Attributes 
•Name 
•Address 
•Value 
•Type 
•Lifetime 
•Scope
Names (Identifiers) 
•Design issues for names: 
–Are names case sensitive? 
–Are special words reserved words or keywords?
Name Forms 
•Length 
–If too short, they cannot be connotative 
–Language examples: 
•FORTRAN I: maximum 6 
•FORTRAN 95: maximum 31 
•C89: unlimited but first 31 is significant 
•Ada, Java, C#: no limit, and all are significant 
•C++: no limit, but implementers often impose one
Name Forms 
•Most of languages: a letter followed by s string consisting of letters, digits, or underscore 
•Special forms: 
–PHP: variables begin with $ 
–Perl: special beginning characters denote type $, @, % 
–Ruby: @ instance variable, @@ class variable
Name Forms 
•Case sensitivity 
–Many languages, including C-based languages, are case sensitive 
–Disadvantage: 
•readability (names that look alike are different) 
•writability: C++ and Java’s predefined names are mixed case (e.g. IndexOutOfBoundsException)
Special Words 
•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 
– A reserved word is a special word that cannot be used as a user-defined name 
–Which one is better? …, but…
Variables 
•A variable is an abstraction of a memory cell 
•Variables can be characterized as 6 attributes: 
–Name 
–Address 
–Type 
–Value 
–Lifetime 
–Scope
Variables Attributes 
•Name - not all variables have them 
•Address - the memory address with which it is associated 
–A variable may have different addresses at different times during execution 
–l-value 
–Two variable names can be used to access the same memory: aliases 
•created via pointers, reference variables, subprogram parameters 
•harmful to readability
Variables Attributes 
•Type - determines the range of values of variables and the set of operations that are defined for values of that type 
•Value - the contents of the memory cell(s) associated with the variable 
–Memory cell here is abstract cell, not physical cell 
–r-value
The Concept of Binding 
•A binding is an association 
–an attribute and an entity 
–an operation and a symbol 
•Binding time is the time at which a binding takes place.
Possible Binding Times 
•Language design time -- bind operator symbols to operations 
•Language implementation time -- data type is bound to range of possible values 
•Compile time -- bind a variable to a data type in Java 
•Load time -- bind a variable to a memory cell for C static variable 
•Link time -- call to library subprogram to its code 
•Runtime -- bind a non-static local variable to a memory cell
Example 
•Consider the C assignment statement 
count = count + 5; 
–Type of count 
–Set of possible values of count 
–Meaning of + 
–Internal representation of 5 
–Value of count
Static and Dynamic Binding 
•A binding is static if it first occurs before run time and remains unchanged throughout program execution. 
•A binding is dynamic if it first occurs during execution or can change during execution of the program
Binding 
•Let’s discuss two types of binding: 
–Type binding: variable to data type 
–Storage binding: variable to its address
Type Binding 
•How is a type specified? 
•When does the binding take place? 
•If static, the type may be specified by either an explicit or an implicit declaration
Explicit/Implicit Declaration 
•An explicit declaration is a program statement used for declaring the types of variables 
•An implicit declaration is a default mechanism for specifying types of variables 
–Ex. 
•FORTRAN: I-N: Integer, others real 
•Perl: $a: scalar, @a: array 
–Advantage: writability 
–Disadvantage: reliability (less trouble with Perl)
Dynamic Type Binding 
•JavaScript and PHP 
•Specified through an assignment statement e.g., JavaScript 
list = [2, 4.33, 6, 8]; 
list = 17.3; 
–Advantage: flexibility (generic program units) 
–Disadvantages: 
•High cost (dynamic type checking and interpretation) 
•Type error detection by the compiler is difficult
Type Inference 
•Ex. ML 
fun circumf(r) = 3.14159 * r * r; 
fun square(x) = x * x; 
fun square( x : real) = x * x; 
fun square(x) = (x : real) * x; 
fun square(x) = x * (x : real); 
fun square(x) : real = x * x;
Storage Binding & Lifetime 
•Storage binding 
–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
Categories of Variables by Lifetimes 
•Static--bound to memory cells before execution begins and remains bound to the same memory cell throughout execution, e.g., all FORTRAN 77 variables, C static variables 
•Advantages: efficiency (direct addressing), history-sensitive subprogram support 
•Disadvantage: lack of flexibility (no recursion)
Categories of Variables by Lifetimes 
•Stack-dynamic--Storage bindings are created for variables when their declaration statements are elaborated. 
–If scalar, all attributes except storage are statically bound 
•local variables in C subprograms and Java methods 
•Advantage: allows recursion; conserves storage 
•Disadvantages: 
–Overhead of allocation and deallocation 
–Subprograms cannot be history sensitive 
–Inefficient references (indirect addressing)
Categories of Variables by Lifetimes 
•Explicit heap-dynamic -- Allocated and deallocated by explicit directives, specified by the programmer, which take effect during execution 
•Referenced only through pointers or references, e.g. dynamic objects in C++ (via new and delete), all objects in Java 
•Advantage: provides for dynamic storage management 
•Disadvantage: inefficient and unreliable
Categories of Variables by Lifetimes 
•Implicit heap-dynamic--Allocation and deallocation caused by assignment statements 
–all variables in APL; all strings and arrays in Perl and JavaScript 
•Advantage: flexibility 
•Disadvantages: 
–Inefficient, because all attributes are dynamic 
–Loss of error detection
Variable Attributes: Scope 
•The scope of a variable is the range of statements over which it is visible 
•The scope rules of a language determine how references to names are associated with variables 
•Local vs. nonlocal variables
Static Scope 
•Based on program text 
•To connect a name reference to a variable, you (or the compiler) must find the declaration 
•Search process: search declarations, first locally, then in increasingly larger enclosing scopes, until one is found for the given name 
•Enclosing static scopes (to a specific scope) are called its static ancestors; the nearest static ancestor is called a static parent
Scope (continued) 
•Variables can be hidden from a unit by having a "closer" variable with the same name 
•C++ and Ada allow access to these "hidden" variables 
–In Ada: unit.name 
–In C++: class_name::name
Example -- Ada 
procedure Big is 
X : Integer; 
procedure Sub1 is 
X : Integer; 
begin -- of Sub1 
... 
end; -- of Sub1 
procedure Sub2 is 
begin -- of Sub2 
... X ... 
end; -- of Sub2 
begin -- of Big 
... 
end; -- of Big
Blocks 
•A method of creating static scopes inside program units--from ALGOL 60 
•Examples: C or C++ (not Java or C#) 
void sub() { 
int count; 
... 
while ( … ) { 
int count; 
count++; 
... 
} 
... 
} 
•Global variables are defined in outermost block
var A, B, C: real; //1 
procedure Sub1 (A: real); //2 
var D: real; 
procedure Sub2 (C: real); 
var D: real; 
begin 
… C:= C+B; … 
end; 
begin 
… Sub2(B); … 
end; 
begin 
… Sub1(A); … 
end. 
Declaration 
Scope 
A:real //1 
Main 
B:real //1 
Main,Sub1,Sub2 
C:real//1 
Main,Sub1 
A:real //2 
Sub1,Sub2 
…
•Assume MAIN calls A and B 
A calls C and D 
B calls A and E 
MAIN 
MAIN 
E 
A 
C 
D 
B 
A 
B C 
D E 
Evaluation of Static Scoping
Static Scope Example 
MAIN 
MAIN 
A 
B 
C 
D 
E 
A 
C 
B 
E 
D
Static Scope (continued) 
•Suppose the spec is changed so that D must now access some data in B 
•Solutions: 
–Put D in B (but then C can no longer call it and D cannot access A's variables) 
–Move the data from B that D needs to MAIN (but then all procedures can access them) 
•Same problem for procedure access 
•Overall: static scoping often encourages many globals
Dynamic Scope 
•Based on calling sequences of program units, not their textual layout 
•References to variables are connected to declarations by searching back through the chain of subprogram calls that forced execution to this point 
•APL, SNOBOL4, early LISP (Perl, Common LISP)
Example 
procedure Big is 
X : Integer; 
procedure Sub1 is 
X : Integer; 
begin -- of Sub1 
... 
end; -- of Sub1 
procedure Sub2 is 
begin -- of Sub2 
... X ... 
end; -- of Sub2 
begin -- of Big 
... 
end; -- of Big 
Big --> Sub1 --> Sub2 
Big --> Sub2
Evaluation 
•Disadvantages: 
–Local variables are not private anymore, less reliable 
–Cannot statically type check 
–Readability 
–Reading overhead 
•Advantage: 
–No need to pass parameters
Scope and Lifetime 
•Scope and lifetime are sometimes closely related, but are different concepts 
•Examples: 
–A variable declared in a Java method that contains no method calls 
–A variable declared in C/C++ function with static specifier 
–Subprogram calls
Referencing Environments 
•The referencing environment of a statement is the collection of all names that are visible in the statement 
•In a static-scoped language, it is the local variables plus all of the visible variables in all of the enclosing scopes
procedure Example is 
A, B : Integer; 
... 
procedure Sub1 is 
X, Y : Integer; 
begin -- of Sub1 
... <-------------------------- 1 
end; -- of Sub1 
procedure Sub2 is 
X : Integer; 
... 
procedure Sub3 is 
X: Integer; 
begin -- of Sub3 
... <----------------------- 2 
end; -- of Sub3 
begin -- of Sub2 
... <------------------------ 3 
end; -- of Sub2 
begin -- of Example 
... <-------------------------- 4 
end. -- of Example
Referencing Environment 
•A subprogram is active if its execution has begun but has not yet terminated 
•In a dynamic-scoped language, the referencing environment is the local variables plus all visible variables in all active subprograms
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 */ 
main --> sub2 --> sub1
Named Constants 
•A named constant is a variable that is bound to a value only when it is bound to storage 
•Advantages: readability and modifiability 
•Used to parameterize programs 
•The binding of values to named constants can be either static (called manifest constants) or dynamic 
•Languages: 
–FORTRAN 90: constant-valued expressions 
–Ada, C++, and Java: expressions of any kind
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, e.g., in Java 
int sum = 0;
Summary 
•Case sensitivity and the relationship of names to special words represent design issues of names 
•Variables are characterized by the sextuples: name, address, value, type, lifetime, scope 
•Binding is the association of attributes with program entities 
•Scalar variables are categorized as: static, stack dynamic, explicit heap dynamic, implicit heap dynamic
Summary (continued) 
•Static scoping provides a simple, reliable, and efficient method of allowing visibility of nonlocal variables in subprograms 
•Dynamic scoping provides more flexibility than static scoping but at expense of readability, reliability, and efficiency 
•Referencing environment of a statement is the collection of all the variables that are visible to that statement

More Related Content

What's hot

Decision making and loop in C#
Decision making and loop in C#Decision making and loop in C#
Decision making and loop in C#
Prasanna Kumar SM
 
Recursion in c++
Recursion in c++Recursion in c++
Recursion in c++
Abdul Rehman
 
Modular programming
Modular programmingModular programming
Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4
shah zeb
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
Kuppusamy P
 
9 subprograms
9 subprograms9 subprograms
9 subprograms
Munawar Ahmed
 
Adnan: Introduction to Natural Language Processing
Adnan: Introduction to Natural Language Processing Adnan: Introduction to Natural Language Processing
Adnan: Introduction to Natural Language Processing Mustafa Jarrar
 
Static and dynamic scoping
Static and dynamic scopingStatic and dynamic scoping
Static and dynamic scoping
NusratShaikh16
 
Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02
Fariz Darari
 
Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)
Paige Bailey
 
Backus Naur and Chomsky Normal Forms
Backus Naur and Chomsky Normal FormsBackus Naur and Chomsky Normal Forms
Backus Naur and Chomsky Normal FormsAshutosh Pandey
 
Python ppt
Python pptPython ppt
Python ppt
Anush verma
 
Programming Language.ppt
Programming Language.pptProgramming Language.ppt
Programming Language.ppt
Lovely Professional University
 
Python Basics.pdf
Python Basics.pdfPython Basics.pdf
Python Basics.pdf
FaizanAli561069
 
Introduction of c programming
Introduction of c programmingIntroduction of c programming
Introduction of c programming
Tarun Sharma
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
Rasan Samarasinghe
 
Complexity analysis - The Big O Notation
Complexity analysis - The Big O NotationComplexity analysis - The Big O Notation
Complexity analysis - The Big O Notation
Jawad Khan
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Shiraz316
 
Constants, Variables, and Data Types
Constants, Variables, and Data TypesConstants, Variables, and Data Types
Constants, Variables, and Data Types
Rokonuzzaman Rony
 

What's hot (20)

Decision making and loop in C#
Decision making and loop in C#Decision making and loop in C#
Decision making and loop in C#
 
Sql 3
Sql 3Sql 3
Sql 3
 
Recursion in c++
Recursion in c++Recursion in c++
Recursion in c++
 
Modular programming
Modular programmingModular programming
Modular programming
 
Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 
9 subprograms
9 subprograms9 subprograms
9 subprograms
 
Adnan: Introduction to Natural Language Processing
Adnan: Introduction to Natural Language Processing Adnan: Introduction to Natural Language Processing
Adnan: Introduction to Natural Language Processing
 
Static and dynamic scoping
Static and dynamic scopingStatic and dynamic scoping
Static and dynamic scoping
 
Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02
 
Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)
 
Backus Naur and Chomsky Normal Forms
Backus Naur and Chomsky Normal FormsBackus Naur and Chomsky Normal Forms
Backus Naur and Chomsky Normal Forms
 
Python ppt
Python pptPython ppt
Python ppt
 
Programming Language.ppt
Programming Language.pptProgramming Language.ppt
Programming Language.ppt
 
Python Basics.pdf
Python Basics.pdfPython Basics.pdf
Python Basics.pdf
 
Introduction of c programming
Introduction of c programmingIntroduction of c programming
Introduction of c programming
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 
Complexity analysis - The Big O Notation
Complexity analysis - The Big O NotationComplexity analysis - The Big O Notation
Complexity analysis - The Big O Notation
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Constants, Variables, and Data Types
Constants, Variables, and Data TypesConstants, Variables, and Data Types
Constants, Variables, and Data Types
 

Viewers also liked

Control structure
Control structureControl structure
Control structure
baran19901990
 
Bus tracking application in Android
Bus tracking application in AndroidBus tracking application in Android
Bus tracking application in Android
yashonil
 
Programming Haskell Chapter8
Programming Haskell Chapter8Programming Haskell Chapter8
Programming Haskell Chapter8
Kousuke Ruichi
 
Algorithms Vs Meta Language
Algorithms Vs Meta LanguageAlgorithms Vs Meta Language
Algorithms Vs Meta LanguageKelly Bauer
 
How to install nginx vs unicorn
How to install nginx vs unicornHow to install nginx vs unicorn
How to install nginx vs unicorn
baran19901990
 
Tìm đường đi xe buýt trong TPHCM bằng Google Map
Tìm đường đi xe buýt trong TPHCM bằng Google MapTìm đường đi xe buýt trong TPHCM bằng Google Map
Tìm đường đi xe buýt trong TPHCM bằng Google Map
baran19901990
 
NS2: Binding C++ and OTcl variables
NS2: Binding C++ and OTcl variablesNS2: Binding C++ and OTcl variables
NS2: Binding C++ and OTcl variablesTeerawat Issariyakul
 
Untitled Presentation
Untitled PresentationUntitled Presentation
Untitled Presentationbaran19901990
 
Scope - Static and Dynamic
Scope - Static and DynamicScope - Static and Dynamic
Scope - Static and Dynamic
Sneh Pahilwani
 
Nhập môn công tác kỹ sư
Nhập môn công tác kỹ sưNhập môn công tác kỹ sư
Nhập môn công tác kỹ sư
baran19901990
 
Config websocket on apache
Config websocket on apacheConfig websocket on apache
Config websocket on apache
baran19901990
 
Principles of programming languages. Detail notes
Principles of programming languages. Detail notesPrinciples of programming languages. Detail notes
Principles of programming languages. Detail notes
VIKAS SINGH BHADOURIA
 
Bus tracking application project report
Bus tracking application project reportBus tracking application project report
Bus tracking application project report
Abhishek Singh
 
Bus Tracking Application in Android
Bus Tracking Application in AndroidBus Tracking Application in Android
Bus Tracking Application in Android
Abhishek Singh
 
Variables: names, bindings, type, scope
Variables: names, bindings, type, scopeVariables: names, bindings, type, scope
Variables: names, bindings, type, scope
suthi
 

Viewers also liked (15)

Control structure
Control structureControl structure
Control structure
 
Bus tracking application in Android
Bus tracking application in AndroidBus tracking application in Android
Bus tracking application in Android
 
Programming Haskell Chapter8
Programming Haskell Chapter8Programming Haskell Chapter8
Programming Haskell Chapter8
 
Algorithms Vs Meta Language
Algorithms Vs Meta LanguageAlgorithms Vs Meta Language
Algorithms Vs Meta Language
 
How to install nginx vs unicorn
How to install nginx vs unicornHow to install nginx vs unicorn
How to install nginx vs unicorn
 
Tìm đường đi xe buýt trong TPHCM bằng Google Map
Tìm đường đi xe buýt trong TPHCM bằng Google MapTìm đường đi xe buýt trong TPHCM bằng Google Map
Tìm đường đi xe buýt trong TPHCM bằng Google Map
 
NS2: Binding C++ and OTcl variables
NS2: Binding C++ and OTcl variablesNS2: Binding C++ and OTcl variables
NS2: Binding C++ and OTcl variables
 
Untitled Presentation
Untitled PresentationUntitled Presentation
Untitled Presentation
 
Scope - Static and Dynamic
Scope - Static and DynamicScope - Static and Dynamic
Scope - Static and Dynamic
 
Nhập môn công tác kỹ sư
Nhập môn công tác kỹ sưNhập môn công tác kỹ sư
Nhập môn công tác kỹ sư
 
Config websocket on apache
Config websocket on apacheConfig websocket on apache
Config websocket on apache
 
Principles of programming languages. Detail notes
Principles of programming languages. Detail notesPrinciples of programming languages. Detail notes
Principles of programming languages. Detail notes
 
Bus tracking application project report
Bus tracking application project reportBus tracking application project report
Bus tracking application project report
 
Bus Tracking Application in Android
Bus Tracking Application in AndroidBus Tracking Application in Android
Bus Tracking Application in Android
 
Variables: names, bindings, type, scope
Variables: names, bindings, type, scopeVariables: names, bindings, type, scope
Variables: names, bindings, type, scope
 

Similar to Subprogram

08 subprograms
08 subprograms08 subprograms
08 subprograms
baran19901990
 
chapter 5.ppt
chapter 5.pptchapter 5.ppt
chapter 5.ppt
ThedronBerhanu
 
5 Names, bindings,Typechecking and Scopes
5 Names, bindings,Typechecking and Scopes5 Names, bindings,Typechecking and Scopes
5 Names, bindings,Typechecking and Scopes
Munawar Ahmed
 
chapter8.ppt clean code Boundary ppt Coding guide
chapter8.ppt clean code Boundary ppt Coding guidechapter8.ppt clean code Boundary ppt Coding guide
chapter8.ppt clean code Boundary ppt Coding guide
SanjeevSaharan5
 
codingtechniques1.ppt
codingtechniques1.pptcodingtechniques1.ppt
codingtechniques1.ppt
azida3
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++
Rasan Samarasinghe
 
Java Closures
Java ClosuresJava Closures
Java Closures
Ben Evans
 
learn JAVA at ASIT with a placement assistance.
learn JAVA at ASIT with a placement assistance.learn JAVA at ASIT with a placement assistance.
learn JAVA at ASIT with a placement assistance.
ASIT Education
 
Java introduction
Java introductionJava introduction
Java introduction
GaneshKumarKanthiah
 
Frp
FrpFrp
Variables in Pharo
Variables in PharoVariables in Pharo
Variables in Pharo
Marcus Denker
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variables
teach4uin
 
Learning from "Effective Scala"
Learning from "Effective Scala"Learning from "Effective Scala"
Learning from "Effective Scala"
Kazuhiro Sera
 
Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8
Simon Ritter
 
TDD and the Legacy Code Black Hole
TDD and the Legacy Code Black HoleTDD and the Legacy Code Black Hole
TDD and the Legacy Code Black Hole
Noam Kfir
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
Heartin Jacob
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_reviewEdureka!
 
Programming Language
Programming  LanguageProgramming  Language
Programming LanguageAdeel Hamid
 
Note for Java Programming////////////////
Note for Java Programming////////////////Note for Java Programming////////////////
Note for Java Programming////////////////
MeghaKulkarni27
 
c++ Unit I.pptx
c++ Unit I.pptxc++ Unit I.pptx

Similar to Subprogram (20)

08 subprograms
08 subprograms08 subprograms
08 subprograms
 
chapter 5.ppt
chapter 5.pptchapter 5.ppt
chapter 5.ppt
 
5 Names, bindings,Typechecking and Scopes
5 Names, bindings,Typechecking and Scopes5 Names, bindings,Typechecking and Scopes
5 Names, bindings,Typechecking and Scopes
 
chapter8.ppt clean code Boundary ppt Coding guide
chapter8.ppt clean code Boundary ppt Coding guidechapter8.ppt clean code Boundary ppt Coding guide
chapter8.ppt clean code Boundary ppt Coding guide
 
codingtechniques1.ppt
codingtechniques1.pptcodingtechniques1.ppt
codingtechniques1.ppt
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++
 
Java Closures
Java ClosuresJava Closures
Java Closures
 
learn JAVA at ASIT with a placement assistance.
learn JAVA at ASIT with a placement assistance.learn JAVA at ASIT with a placement assistance.
learn JAVA at ASIT with a placement assistance.
 
Java introduction
Java introductionJava introduction
Java introduction
 
Frp
FrpFrp
Frp
 
Variables in Pharo
Variables in PharoVariables in Pharo
Variables in Pharo
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variables
 
Learning from "Effective Scala"
Learning from "Effective Scala"Learning from "Effective Scala"
Learning from "Effective Scala"
 
Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8
 
TDD and the Legacy Code Black Hole
TDD and the Legacy Code Black HoleTDD and the Legacy Code Black Hole
TDD and the Legacy Code Black Hole
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_review
 
Programming Language
Programming  LanguageProgramming  Language
Programming Language
 
Note for Java Programming////////////////
Note for Java Programming////////////////Note for Java Programming////////////////
Note for Java Programming////////////////
 
c++ Unit I.pptx
c++ Unit I.pptxc++ Unit I.pptx
c++ Unit I.pptx
 

More from baran19901990

How to build a news website use CMS wordpress
How to build a news website use CMS wordpressHow to build a news website use CMS wordpress
How to build a news website use CMS wordpress
baran19901990
 
Lexical
LexicalLexical
Lexical
baran19901990
 
Introduction
IntroductionIntroduction
Introduction
baran19901990
 
Datatype
DatatypeDatatype
Datatype
baran19901990
 
10 logic+programming+with+prolog
10 logic+programming+with+prolog10 logic+programming+with+prolog
10 logic+programming+with+prolog
baran19901990
 
09 implementing+subprograms
09 implementing+subprograms09 implementing+subprograms
09 implementing+subprograms
baran19901990
 
07 control+structures
07 control+structures07 control+structures
07 control+structures
baran19901990
 
How to install git on ubuntu
How to install git on ubuntuHow to install git on ubuntu
How to install git on ubuntu
baran19901990
 
Báo cáo mô hình quản lý khách sạn
Báo cáo mô hình quản lý khách sạnBáo cáo mô hình quản lý khách sạn
Báo cáo mô hình quản lý khách sạnbaran19901990
 

More from baran19901990 (16)

How to build a news website use CMS wordpress
How to build a news website use CMS wordpressHow to build a news website use CMS wordpress
How to build a news website use CMS wordpress
 
Lexical
LexicalLexical
Lexical
 
Introduction
IntroductionIntroduction
Introduction
 
Datatype
DatatypeDatatype
Datatype
 
10 logic+programming+with+prolog
10 logic+programming+with+prolog10 logic+programming+with+prolog
10 logic+programming+with+prolog
 
09 implementing+subprograms
09 implementing+subprograms09 implementing+subprograms
09 implementing+subprograms
 
07 control+structures
07 control+structures07 control+structures
07 control+structures
 
How to install git on ubuntu
How to install git on ubuntuHow to install git on ubuntu
How to install git on ubuntu
 
Ruby notification
Ruby notificationRuby notification
Ruby notification
 
Rails notification
Rails notificationRails notification
Rails notification
 
Linux notification
Linux notificationLinux notification
Linux notification
 
Lab4
Lab4Lab4
Lab4
 
Lab5
Lab5Lab5
Lab5
 
09
0909
09
 
Báo cáo mô hình quản lý khách sạn
Báo cáo mô hình quản lý khách sạnBáo cáo mô hình quản lý khách sạn
Báo cáo mô hình quản lý khách sạn
 
MDA Framework
MDA FrameworkMDA Framework
MDA Framework
 

Recently uploaded

1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...
JeyaPerumal1
 
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
ufdana
 
Comptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guideComptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guide
GTProductions1
 
Latest trends in computer networking.pptx
Latest trends in computer networking.pptxLatest trends in computer networking.pptx
Latest trends in computer networking.pptx
JungkooksNonexistent
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
Gal Baras
 
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdfMeet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
Florence Consulting
 
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
laozhuseo02
 
Italy Agriculture Equipment Market Outlook to 2027
Italy Agriculture Equipment Market Outlook to 2027Italy Agriculture Equipment Market Outlook to 2027
Italy Agriculture Equipment Market Outlook to 2027
harveenkaur52
 
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
3ipehhoa
 
Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...
Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...
Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...
CIOWomenMagazine
 
Bài tập unit 1 English in the world.docx
Bài tập unit 1 English in the world.docxBài tập unit 1 English in the world.docx
Bài tập unit 1 English in the world.docx
nhiyenphan2005
 
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
3ipehhoa
 
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC
 
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
eutxy
 
The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptx
laozhuseo02
 
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptxBridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Brad Spiegel Macon GA
 
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
3ipehhoa
 
This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!
nirahealhty
 
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
keoku
 
guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...
Rogerio Filho
 

Recently uploaded (20)

1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...
 
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
 
Comptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guideComptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guide
 
Latest trends in computer networking.pptx
Latest trends in computer networking.pptxLatest trends in computer networking.pptx
Latest trends in computer networking.pptx
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
 
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdfMeet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
Meet up Milano 14 _ Axpo Italia_ Migration from Mule3 (On-prem) to.pdf
 
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
 
Italy Agriculture Equipment Market Outlook to 2027
Italy Agriculture Equipment Market Outlook to 2027Italy Agriculture Equipment Market Outlook to 2027
Italy Agriculture Equipment Market Outlook to 2027
 
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
 
Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...
Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...
Internet of Things in Manufacturing: Revolutionizing Efficiency & Quality | C...
 
Bài tập unit 1 English in the world.docx
Bài tập unit 1 English in the world.docxBài tập unit 1 English in the world.docx
Bài tập unit 1 English in the world.docx
 
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
 
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
 
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
 
The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptx
 
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptxBridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
 
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
 
This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!
 
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
 
guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...
 

Subprogram

  • 1. Chapter 5: Names, Bindings, and Scopes Principles of Programming Languages
  • 2. Contents •Names •Variables •The Concept of Binding •Scope and Lifetime •Referencing Environments •Named Constants
  • 3. Introduction •Imperative languages are abstractions of von Neumann architecture –Memory  variables •Variables characterized by attributes, among of them is type •Scope and lifetime of variables are important issues when designing type
  • 4. Variable Attributes •Name •Address •Value •Type •Lifetime •Scope
  • 5. Names (Identifiers) •Design issues for names: –Are names case sensitive? –Are special words reserved words or keywords?
  • 6. Name Forms •Length –If too short, they cannot be connotative –Language examples: •FORTRAN I: maximum 6 •FORTRAN 95: maximum 31 •C89: unlimited but first 31 is significant •Ada, Java, C#: no limit, and all are significant •C++: no limit, but implementers often impose one
  • 7. Name Forms •Most of languages: a letter followed by s string consisting of letters, digits, or underscore •Special forms: –PHP: variables begin with $ –Perl: special beginning characters denote type $, @, % –Ruby: @ instance variable, @@ class variable
  • 8. Name Forms •Case sensitivity –Many languages, including C-based languages, are case sensitive –Disadvantage: •readability (names that look alike are different) •writability: C++ and Java’s predefined names are mixed case (e.g. IndexOutOfBoundsException)
  • 9. Special Words •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 – A reserved word is a special word that cannot be used as a user-defined name –Which one is better? …, but…
  • 10. Variables •A variable is an abstraction of a memory cell •Variables can be characterized as 6 attributes: –Name –Address –Type –Value –Lifetime –Scope
  • 11. Variables Attributes •Name - not all variables have them •Address - the memory address with which it is associated –A variable may have different addresses at different times during execution –l-value –Two variable names can be used to access the same memory: aliases •created via pointers, reference variables, subprogram parameters •harmful to readability
  • 12. Variables Attributes •Type - determines the range of values of variables and the set of operations that are defined for values of that type •Value - the contents of the memory cell(s) associated with the variable –Memory cell here is abstract cell, not physical cell –r-value
  • 13. The Concept of Binding •A binding is an association –an attribute and an entity –an operation and a symbol •Binding time is the time at which a binding takes place.
  • 14. Possible Binding Times •Language design time -- bind operator symbols to operations •Language implementation time -- data type is bound to range of possible values •Compile time -- bind a variable to a data type in Java •Load time -- bind a variable to a memory cell for C static variable •Link time -- call to library subprogram to its code •Runtime -- bind a non-static local variable to a memory cell
  • 15. Example •Consider the C assignment statement count = count + 5; –Type of count –Set of possible values of count –Meaning of + –Internal representation of 5 –Value of count
  • 16. Static and Dynamic Binding •A binding is static if it first occurs before run time and remains unchanged throughout program execution. •A binding is dynamic if it first occurs during execution or can change during execution of the program
  • 17. Binding •Let’s discuss two types of binding: –Type binding: variable to data type –Storage binding: variable to its address
  • 18. Type Binding •How is a type specified? •When does the binding take place? •If static, the type may be specified by either an explicit or an implicit declaration
  • 19. Explicit/Implicit Declaration •An explicit declaration is a program statement used for declaring the types of variables •An implicit declaration is a default mechanism for specifying types of variables –Ex. •FORTRAN: I-N: Integer, others real •Perl: $a: scalar, @a: array –Advantage: writability –Disadvantage: reliability (less trouble with Perl)
  • 20. Dynamic Type Binding •JavaScript and PHP •Specified through an assignment statement e.g., JavaScript list = [2, 4.33, 6, 8]; list = 17.3; –Advantage: flexibility (generic program units) –Disadvantages: •High cost (dynamic type checking and interpretation) •Type error detection by the compiler is difficult
  • 21. Type Inference •Ex. ML fun circumf(r) = 3.14159 * r * r; fun square(x) = x * x; fun square( x : real) = x * x; fun square(x) = (x : real) * x; fun square(x) = x * (x : real); fun square(x) : real = x * x;
  • 22. Storage Binding & Lifetime •Storage binding –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
  • 23. Categories of Variables by Lifetimes •Static--bound to memory cells before execution begins and remains bound to the same memory cell throughout execution, e.g., all FORTRAN 77 variables, C static variables •Advantages: efficiency (direct addressing), history-sensitive subprogram support •Disadvantage: lack of flexibility (no recursion)
  • 24. Categories of Variables by Lifetimes •Stack-dynamic--Storage bindings are created for variables when their declaration statements are elaborated. –If scalar, all attributes except storage are statically bound •local variables in C subprograms and Java methods •Advantage: allows recursion; conserves storage •Disadvantages: –Overhead of allocation and deallocation –Subprograms cannot be history sensitive –Inefficient references (indirect addressing)
  • 25. Categories of Variables by Lifetimes •Explicit heap-dynamic -- Allocated and deallocated by explicit directives, specified by the programmer, which take effect during execution •Referenced only through pointers or references, e.g. dynamic objects in C++ (via new and delete), all objects in Java •Advantage: provides for dynamic storage management •Disadvantage: inefficient and unreliable
  • 26. Categories of Variables by Lifetimes •Implicit heap-dynamic--Allocation and deallocation caused by assignment statements –all variables in APL; all strings and arrays in Perl and JavaScript •Advantage: flexibility •Disadvantages: –Inefficient, because all attributes are dynamic –Loss of error detection
  • 27. Variable Attributes: Scope •The scope of a variable is the range of statements over which it is visible •The scope rules of a language determine how references to names are associated with variables •Local vs. nonlocal variables
  • 28. Static Scope •Based on program text •To connect a name reference to a variable, you (or the compiler) must find the declaration •Search process: search declarations, first locally, then in increasingly larger enclosing scopes, until one is found for the given name •Enclosing static scopes (to a specific scope) are called its static ancestors; the nearest static ancestor is called a static parent
  • 29. Scope (continued) •Variables can be hidden from a unit by having a "closer" variable with the same name •C++ and Ada allow access to these "hidden" variables –In Ada: unit.name –In C++: class_name::name
  • 30. Example -- Ada procedure Big is X : Integer; procedure Sub1 is X : Integer; begin -- of Sub1 ... end; -- of Sub1 procedure Sub2 is begin -- of Sub2 ... X ... end; -- of Sub2 begin -- of Big ... end; -- of Big
  • 31. Blocks •A method of creating static scopes inside program units--from ALGOL 60 •Examples: C or C++ (not Java or C#) void sub() { int count; ... while ( … ) { int count; count++; ... } ... } •Global variables are defined in outermost block
  • 32. var A, B, C: real; //1 procedure Sub1 (A: real); //2 var D: real; procedure Sub2 (C: real); var D: real; begin … C:= C+B; … end; begin … Sub2(B); … end; begin … Sub1(A); … end. Declaration Scope A:real //1 Main B:real //1 Main,Sub1,Sub2 C:real//1 Main,Sub1 A:real //2 Sub1,Sub2 …
  • 33. •Assume MAIN calls A and B A calls C and D B calls A and E MAIN MAIN E A C D B A B C D E Evaluation of Static Scoping
  • 34. Static Scope Example MAIN MAIN A B C D E A C B E D
  • 35. Static Scope (continued) •Suppose the spec is changed so that D must now access some data in B •Solutions: –Put D in B (but then C can no longer call it and D cannot access A's variables) –Move the data from B that D needs to MAIN (but then all procedures can access them) •Same problem for procedure access •Overall: static scoping often encourages many globals
  • 36. Dynamic Scope •Based on calling sequences of program units, not their textual layout •References to variables are connected to declarations by searching back through the chain of subprogram calls that forced execution to this point •APL, SNOBOL4, early LISP (Perl, Common LISP)
  • 37. Example procedure Big is X : Integer; procedure Sub1 is X : Integer; begin -- of Sub1 ... end; -- of Sub1 procedure Sub2 is begin -- of Sub2 ... X ... end; -- of Sub2 begin -- of Big ... end; -- of Big Big --> Sub1 --> Sub2 Big --> Sub2
  • 38. Evaluation •Disadvantages: –Local variables are not private anymore, less reliable –Cannot statically type check –Readability –Reading overhead •Advantage: –No need to pass parameters
  • 39. Scope and Lifetime •Scope and lifetime are sometimes closely related, but are different concepts •Examples: –A variable declared in a Java method that contains no method calls –A variable declared in C/C++ function with static specifier –Subprogram calls
  • 40. Referencing Environments •The referencing environment of a statement is the collection of all names that are visible in the statement •In a static-scoped language, it is the local variables plus all of the visible variables in all of the enclosing scopes
  • 41. procedure Example is A, B : Integer; ... procedure Sub1 is X, Y : Integer; begin -- of Sub1 ... <-------------------------- 1 end; -- of Sub1 procedure Sub2 is X : Integer; ... procedure Sub3 is X: Integer; begin -- of Sub3 ... <----------------------- 2 end; -- of Sub3 begin -- of Sub2 ... <------------------------ 3 end; -- of Sub2 begin -- of Example ... <-------------------------- 4 end. -- of Example
  • 42. Referencing Environment •A subprogram is active if its execution has begun but has not yet terminated •In a dynamic-scoped language, the referencing environment is the local variables plus all visible variables in all active subprograms
  • 43. 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 */ main --> sub2 --> sub1
  • 44. Named Constants •A named constant is a variable that is bound to a value only when it is bound to storage •Advantages: readability and modifiability •Used to parameterize programs •The binding of values to named constants can be either static (called manifest constants) or dynamic •Languages: –FORTRAN 90: constant-valued expressions –Ada, C++, and Java: expressions of any kind
  • 45. 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, e.g., in Java int sum = 0;
  • 46. Summary •Case sensitivity and the relationship of names to special words represent design issues of names •Variables are characterized by the sextuples: name, address, value, type, lifetime, scope •Binding is the association of attributes with program entities •Scalar variables are categorized as: static, stack dynamic, explicit heap dynamic, implicit heap dynamic
  • 47. Summary (continued) •Static scoping provides a simple, reliable, and efficient method of allowing visibility of nonlocal variables in subprograms •Dynamic scoping provides more flexibility than static scoping but at expense of readability, reliability, and efficiency •Referencing environment of a statement is the collection of all the variables that are visible to that statement