SlideShare a Scribd company logo
1 of 33
Names, Bindings, and Scopes
CP 111 Lecture No. 3
OUTLINE
● Names,
● Variables,
● Variable declarations,
● The Concept of Binding,
● Scope, Scope and Lifetime,
● Referencing Environments, and Named
Constants.
Names
The term identifier is often used interchangeably
with name.
Names
Design Issues
The following are the primary design issues for names:
● Are names case sensitive?
● Are the special words of the language reserved words or
keywords?
Name….
● 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
( _ ).
● Fortran 95+ allows up to 31 characters in its names.
● Names in Java, C#, and Ada have no length limit, and all characters in them
are significant. C++ does not specify a length limit on names, although
implementors sometimes do.
● Note that the use of underscores and mixed case in names is a
programming style issue, not a language design issue.
Name….
● All variable names in PHP must begin with a dollar sign.
● In Perl, the special character at the beginning of a variable’s name, $,
@, or %, specifies its type (although in a different sense than in other
languages).
● In Ruby, special characters at the beginning of a variable’s name, @
or @@, indicate that the variable is an instance or a class variable,
respectively.
Name….
● In many languages, notably the C-based languages, uppercase and
lowercase letters in names are distinct; that is, names in these languages
are case sensitive.
● For example, the following three names are distinct in C++: rose, ROSE, and
Rose.
● To some people, this is a serious detriment to readability, because names
that look very similar in fact denote different entities. In that sense, case
sensitivity violates the design principle that language constructs that look
similar should have similar meanings. But in languages whose variable
names are case-sensitive, although Rose and rose look similar, there is no
connection between them.
Name….
● 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#, however, 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.
● It is a kind of intolerance on the part of the language designer, which is
enforced by the compiler.
Question?
Special Words
● In most languages, special words are classified as reserved words, which
means they cannot be redefined by programmers, but in some they are only
keywords, which means they can be redefined.
● A keyword is a word of a programming language that is special only in
certain contexts.
● A reserved word is a special word of a programming language that cannot be
used as a name.
● As a language design choice, reserved words are better than keywords
because the ability to redefine keywords can be confusing.
Special Words
● In most languages, names that are defined in other program units, such as
Java packages and C and C++ libraries, can be made visible to a program.
● These names are predefined, but visible only if explicitly imported. Once
imported, they cannot be redefined.
Variables
Variable
● A program variable is an abstraction of a computer memory cell or collection
of cells.
● Programmers often think of variable names as names for memory locations,
but there is much more to a variable than just a name.
● The move from machine languages to assembly languages was largely one
of replacing absolute numeric memory addresses for data with names,
making programs far more readable and therefore easier to write and
maintain.
● That step also provided an escape from the problem of manual absolute
addressing, because the translator that converted the names to actual
addresses also chose those addresses.
Variable
● A variable can be characterized as a sextuple of attributes: (name, address,
value, type, lifetime, and scope).
● Although this may seem too complicated for such an apparently simple
concept, it provides the clearest way to explain the various aspects of
variables.
● Our discussion of variable attributes will lead to examinations of the
important related concepts of aliases, binding, binding times, declarations,
scoping rules, and referencing environments.
sextuple attributes of a Variable:
• name,
• address,
• value,
• type,
• lifetime, and
• scope.
Name
● Variable names are the most common names in programs.
Address
● The address of a variable is the machine memory address with which it is
associated.
● This association is not as simple as it may at first appear.
● In many languages, it is possible for the same variable to be associated with
different addresses at different times in the program.
● It is possible to have multiple variables that have the same address.
● When more than one variable name can be used to access the same memory
location, the variables are called aliases.
Aliasing
● Aliasing is a hindrance to readability because it allows a variable to have its
value changed by an assignment to a different variable.
● For example, if variables named total and sum are aliases, any change to the
value of total also changes the value of sum and vice versa.
● A reader of the program must always remember that total and sum are
different names for the same memory cell.
● Because there can be any number of aliases in a program, this may be very
difficult in practice.
● Aliasing also makes program verification more difficult.
Type
● The type of a variable determines the range of values the variable can store
and the set of operations that are defined for values of the type.
● For example, the int type in Java specifies a value range of -2147483648 to
2147483647 and arithmetic operations for addition, subtraction,
multiplication, division, and modulus.
Value
● The value of a variable is the contents of the memory cell or cells
associated with the variable.
● It is convenient to think of computer memory in terms of abstract
cells, rather than physical cells.
● The physical cells, or individually addressable units, of most
contemporary computer memories are byte-size, with a byte usually
being eight bits in length.
Binding
● A binding is an association between an attribute and an entity, such as
between a variable and its type or value, or between an operation and a
symbol.
● The time at which a binding takes place is called binding time.
● Binding and binding times are prominent concepts in the semantics of
programming languages.
● Bindings can take place at language design time, language implementation
● time, compile time, load time, link time, or run time.
● For example, the asterisk symbol (*) is usually bound to the multiplication
operation at language design time. A data type, such as int in C, is bound to
a range of possible values at language implementation time.
Binding of Attributes to Variables
● A binding is static if it first occurs before run time begins and remains
unchanged throughout program execution.
● If the binding first occurs during run time or can change in the course of
program execution, it is called dynamic.
● The physical binding of a variable to a storage cell in a virtual memory
environment is complex, because the page or segment of the address space
in which the cell resides may be moved in and out of memory many times
during program execution.
Type Bindings
● Before a variable can be referenced in a program, it must be bound to a data
type.
● The two important aspects of this binding are how the type is specified and
when the binding takes place.
● Types can be specified statically through some form of explicit or implicit
declaration.
Type Bindings….
● An explicit declaration is a statement in a program that lists variable names
and specifies that they are a particular type.
● An implicit declaration is a means of associating variables with types
through default conventions, rather than declaration statements. In this
case, the first appearance of a variable name in a program constitutes its
implicit declaration.
● Both explicit and implicit declarations create static bindings to types.
Type Bindings….
Dynamic Type Binding
● With dynamic type binding, the type of a variable is not specified by a
declaration statement, nor can it be determined by the spelling of its name.
Instead, the variable is bound to a type when it is assigned a value in an
assignment statement.
● When the assignment statement is executed, the variable being assigned is
bound to the type of the value of the expression on the right side of the
nassignment.
● Such an assignment may also bind the variable to an address and a memory
cell, because different type values may require different amounts of
storage.
Storage Bindings and Lifetime
● Allocation: The memory cell to which a variable is bound somehow must be
taken from a pool of available memory.
● Deallocation is the process of placing a memory cell that has been unbound
from a variable back into the pool of available memory.
● The lifetime of a variable is the time during which the variable is bound to a
specific memory location.
● So, the lifetime of a variable begins when it is bound to a specific cell and
ends when it is unbound from that cell.
Static variables
● Static variables are those that are bound to memory cells before program
execution begins and remain bound to those same memory cells until
program execution terminates.
Local and Global Scope
● Local Scope: program structure that is a sequence of function
definitions, in which variable definitions can appear inside the
functions.
● Global scope Some languages, including C, C++, PHP, JavaScript,
and Python, allow a program structure that is a sequence of function
definitions, in which variable definitions can appear outside the
functions. Definitions outside functions in a file create global
variables, which potentially can be visible to those functions.
Scope and Lifetime
● Sometimes the scope and lifetime of a variable appear to be related.
Referencing Environments
● The referencing environment of a statement is the collection of all variables
● that are visible in the statement.
● The referencing environment of a statement in a static-scoped language is
the variables declared in its local scope plus the collection of all variables of
its ancestor scopes that are visible.
● In such a language, the referencing environment of a statement is needed
while that statement is being compiled, so code and data structures can be
created to allow references to variables from other scopes during run time.
Named constant
● A named constant is a variable that is bound to a value only once.
● Named constants are useful as aids to readability and program reliability.
● Readability can be improved, for example, by using the name pi instead of
the constant 3.14159265.
Questions?
END

More Related Content

What's hot

Principles of-programming-languages-lecture-notes-
Principles of-programming-languages-lecture-notes-Principles of-programming-languages-lecture-notes-
Principles of-programming-languages-lecture-notes-Krishna Sai
 
From Programming to Modeling And Back Again
From Programming to Modeling And Back AgainFrom Programming to Modeling And Back Again
From Programming to Modeling And Back AgainMarkus Voelter
 
The GO programming language
The GO programming languageThe GO programming language
The GO programming languageMarco Sabatini
 
Architecting Domain-Specific Languages
Architecting Domain-Specific LanguagesArchitecting Domain-Specific Languages
Architecting Domain-Specific LanguagesMarkus Voelter
 
Perl Development (Sample Courseware)
Perl Development (Sample Courseware)Perl Development (Sample Courseware)
Perl Development (Sample Courseware)Garth Gilmour
 
New c sharp4_features_part_iv
New c sharp4_features_part_ivNew c sharp4_features_part_iv
New c sharp4_features_part_ivNico Ludwig
 
C programming language Reference Note
C programming language Reference NoteC programming language Reference Note
C programming language Reference NoteChetan Thapa Magar
 
New c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_iiNew c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_iiNico Ludwig
 
Password protected diary
Password protected diaryPassword protected diary
Password protected diarySHARDA SHARAN
 
over all view programming to computer
over all view programming to computer over all view programming to computer
over all view programming to computer muniryaseen
 
Dart PPT.pptx
Dart PPT.pptxDart PPT.pptx
Dart PPT.pptxDSCMESCOE
 
Programming In C++
Programming In C++ Programming In C++
Programming In C++ shammi mehra
 

What's hot (19)

Principles of-programming-languages-lecture-notes-
Principles of-programming-languages-lecture-notes-Principles of-programming-languages-lecture-notes-
Principles of-programming-languages-lecture-notes-
 
Programming Languages
Programming LanguagesProgramming Languages
Programming Languages
 
From Programming to Modeling And Back Again
From Programming to Modeling And Back AgainFrom Programming to Modeling And Back Again
From Programming to Modeling And Back Again
 
The GO programming language
The GO programming languageThe GO programming language
The GO programming language
 
Paradigms
ParadigmsParadigms
Paradigms
 
Architecting Domain-Specific Languages
Architecting Domain-Specific LanguagesArchitecting Domain-Specific Languages
Architecting Domain-Specific Languages
 
Perl Development (Sample Courseware)
Perl Development (Sample Courseware)Perl Development (Sample Courseware)
Perl Development (Sample Courseware)
 
New c sharp4_features_part_iv
New c sharp4_features_part_ivNew c sharp4_features_part_iv
New c sharp4_features_part_iv
 
C programming language Reference Note
C programming language Reference NoteC programming language Reference Note
C programming language Reference Note
 
New c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_iiNew c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_ii
 
Password protected diary
Password protected diaryPassword protected diary
Password protected diary
 
over all view programming to computer
over all view programming to computer over all view programming to computer
over all view programming to computer
 
Dart PPT.pptx
Dart PPT.pptxDart PPT.pptx
Dart PPT.pptx
 
Introduction to programming languages part 1
Introduction to programming languages   part 1Introduction to programming languages   part 1
Introduction to programming languages part 1
 
Features of c
Features of cFeatures of c
Features of c
 
Programming In C++
Programming In C++ Programming In C++
Programming In C++
 
OOP Poster Presentation
OOP Poster PresentationOOP Poster Presentation
OOP Poster Presentation
 
Introduction to programming c
Introduction to programming cIntroduction to programming c
Introduction to programming c
 
Introduction to ‘C’ Language
Introduction to ‘C’ LanguageIntroduction to ‘C’ Language
Introduction to ‘C’ Language
 

Similar to Cp 111 lecture 3

358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1sumitbardhan
 
A program _ is an abstraction in a programming language for the memor.pdf
A program _  is an abstraction in a programming language for the memor.pdfA program _  is an abstraction in a programming language for the memor.pdf
A program _ is an abstraction in a programming language for the memor.pdfshashanth1188
 
1.2 Evaluation of PLs.ppt
1.2 Evaluation of PLs.ppt1.2 Evaluation of PLs.ppt
1.2 Evaluation of PLs.pptmeenabairagi1
 
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdfL2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdfMMRF2
 
Using Variables in Programming
Using Variables in ProgrammingUsing Variables in Programming
Using Variables in Programmingflippanthorse6864
 
Using Variables in Programming
Using Variables in ProgrammingUsing Variables in Programming
Using Variables in Programmingabortivepyramid08
 
Using Variables in Programming
Using Variables in ProgrammingUsing Variables in Programming
Using Variables in Programmingunadvisednerve735
 
Programming languages and concepts by vivek parihar
Programming languages and concepts by vivek pariharProgramming languages and concepts by vivek parihar
Programming languages and concepts by vivek pariharVivek Parihar
 
Switch case and looping statement
Switch case and looping statementSwitch case and looping statement
Switch case and looping statement_jenica
 
Introduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdfIntroduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdfAbrehamKassa
 
Pattern-Level Programming with Asteroid
Pattern-Level Programming with AsteroidPattern-Level Programming with Asteroid
Pattern-Level Programming with Asteroidijpla
 

Similar to Cp 111 lecture 3 (20)

Unit 1
Unit 1Unit 1
Unit 1
 
358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1
 
A program _ is an abstraction in a programming language for the memor.pdf
A program _  is an abstraction in a programming language for the memor.pdfA program _  is an abstraction in a programming language for the memor.pdf
A program _ is an abstraction in a programming language for the memor.pdf
 
1.2 Evaluation of PLs.ppt
1.2 Evaluation of PLs.ppt1.2 Evaluation of PLs.ppt
1.2 Evaluation of PLs.ppt
 
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdfL2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
 
Using Variables in Programming
Using Variables in ProgrammingUsing Variables in Programming
Using Variables in Programming
 
Using Variables in Programming
Using Variables in ProgrammingUsing Variables in Programming
Using Variables in Programming
 
Coding conventions
Coding conventionsCoding conventions
Coding conventions
 
Using Variables in Programming
Using Variables in ProgrammingUsing Variables in Programming
Using Variables in Programming
 
C tokens
C tokensC tokens
C tokens
 
Programming languages and concepts by vivek parihar
Programming languages and concepts by vivek pariharProgramming languages and concepts by vivek parihar
Programming languages and concepts by vivek parihar
 
Golang online course
Golang online courseGolang online course
Golang online course
 
8844632.ppt
8844632.ppt8844632.ppt
8844632.ppt
 
Switch case and looping statement
Switch case and looping statementSwitch case and looping statement
Switch case and looping statement
 
Introduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdfIntroduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdf
 
Subprogram
SubprogramSubprogram
Subprogram
 
Pattern-Level Programming with Asteroid
Pattern-Level Programming with AsteroidPattern-Level Programming with Asteroid
Pattern-Level Programming with Asteroid
 
history of c.ppt
history of c.ppthistory of c.ppt
history of c.ppt
 
Unit 2.pptx
Unit 2.pptxUnit 2.pptx
Unit 2.pptx
 
Unit 2.pptx
Unit 2.pptxUnit 2.pptx
Unit 2.pptx
 

Recently uploaded

Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 

Recently uploaded (20)

Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 

Cp 111 lecture 3

  • 1. Names, Bindings, and Scopes CP 111 Lecture No. 3
  • 2. OUTLINE ● Names, ● Variables, ● Variable declarations, ● The Concept of Binding, ● Scope, Scope and Lifetime, ● Referencing Environments, and Named Constants.
  • 3. Names The term identifier is often used interchangeably with name.
  • 4. Names Design Issues The following are the primary design issues for names: ● Are names case sensitive? ● Are the special words of the language reserved words or keywords?
  • 5. Name…. ● 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 ( _ ). ● Fortran 95+ allows up to 31 characters in its names. ● Names in Java, C#, and Ada have no length limit, and all characters in them are significant. C++ does not specify a length limit on names, although implementors sometimes do. ● Note that the use of underscores and mixed case in names is a programming style issue, not a language design issue.
  • 6. Name…. ● All variable names in PHP must begin with a dollar sign. ● In Perl, the special character at the beginning of a variable’s name, $, @, or %, specifies its type (although in a different sense than in other languages). ● In Ruby, special characters at the beginning of a variable’s name, @ or @@, indicate that the variable is an instance or a class variable, respectively.
  • 7. Name…. ● In many languages, notably the C-based languages, uppercase and lowercase letters in names are distinct; that is, names in these languages are case sensitive. ● For example, the following three names are distinct in C++: rose, ROSE, and Rose. ● To some people, this is a serious detriment to readability, because names that look very similar in fact denote different entities. In that sense, case sensitivity violates the design principle that language constructs that look similar should have similar meanings. But in languages whose variable names are case-sensitive, although Rose and rose look similar, there is no connection between them.
  • 8. Name…. ● 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#, however, 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. ● It is a kind of intolerance on the part of the language designer, which is enforced by the compiler.
  • 10. Special Words ● In most languages, special words are classified as reserved words, which means they cannot be redefined by programmers, but in some they are only keywords, which means they can be redefined. ● A keyword is a word of a programming language that is special only in certain contexts. ● A reserved word is a special word of a programming language that cannot be used as a name. ● As a language design choice, reserved words are better than keywords because the ability to redefine keywords can be confusing.
  • 11. Special Words ● In most languages, names that are defined in other program units, such as Java packages and C and C++ libraries, can be made visible to a program. ● These names are predefined, but visible only if explicitly imported. Once imported, they cannot be redefined.
  • 13. Variable ● A program variable is an abstraction of a computer memory cell or collection of cells. ● Programmers often think of variable names as names for memory locations, but there is much more to a variable than just a name. ● The move from machine languages to assembly languages was largely one of replacing absolute numeric memory addresses for data with names, making programs far more readable and therefore easier to write and maintain. ● That step also provided an escape from the problem of manual absolute addressing, because the translator that converted the names to actual addresses also chose those addresses.
  • 14. Variable ● A variable can be characterized as a sextuple of attributes: (name, address, value, type, lifetime, and scope). ● Although this may seem too complicated for such an apparently simple concept, it provides the clearest way to explain the various aspects of variables. ● Our discussion of variable attributes will lead to examinations of the important related concepts of aliases, binding, binding times, declarations, scoping rules, and referencing environments.
  • 15. sextuple attributes of a Variable: • name, • address, • value, • type, • lifetime, and • scope.
  • 16. Name ● Variable names are the most common names in programs.
  • 17. Address ● The address of a variable is the machine memory address with which it is associated. ● This association is not as simple as it may at first appear. ● In many languages, it is possible for the same variable to be associated with different addresses at different times in the program. ● It is possible to have multiple variables that have the same address. ● When more than one variable name can be used to access the same memory location, the variables are called aliases.
  • 18. Aliasing ● Aliasing is a hindrance to readability because it allows a variable to have its value changed by an assignment to a different variable. ● For example, if variables named total and sum are aliases, any change to the value of total also changes the value of sum and vice versa. ● A reader of the program must always remember that total and sum are different names for the same memory cell. ● Because there can be any number of aliases in a program, this may be very difficult in practice. ● Aliasing also makes program verification more difficult.
  • 19. Type ● The type of a variable determines the range of values the variable can store and the set of operations that are defined for values of the type. ● For example, the int type in Java specifies a value range of -2147483648 to 2147483647 and arithmetic operations for addition, subtraction, multiplication, division, and modulus.
  • 20. Value ● The value of a variable is the contents of the memory cell or cells associated with the variable. ● It is convenient to think of computer memory in terms of abstract cells, rather than physical cells. ● The physical cells, or individually addressable units, of most contemporary computer memories are byte-size, with a byte usually being eight bits in length.
  • 21. Binding ● A binding is an association between an attribute and an entity, such as between a variable and its type or value, or between an operation and a symbol. ● The time at which a binding takes place is called binding time. ● Binding and binding times are prominent concepts in the semantics of programming languages. ● Bindings can take place at language design time, language implementation ● time, compile time, load time, link time, or run time. ● For example, the asterisk symbol (*) is usually bound to the multiplication operation at language design time. A data type, such as int in C, is bound to a range of possible values at language implementation time.
  • 22. Binding of Attributes to Variables ● A binding is static if it first occurs before run time begins and remains unchanged throughout program execution. ● If the binding first occurs during run time or can change in the course of program execution, it is called dynamic. ● The physical binding of a variable to a storage cell in a virtual memory environment is complex, because the page or segment of the address space in which the cell resides may be moved in and out of memory many times during program execution.
  • 23. Type Bindings ● Before a variable can be referenced in a program, it must be bound to a data type. ● The two important aspects of this binding are how the type is specified and when the binding takes place. ● Types can be specified statically through some form of explicit or implicit declaration.
  • 24. Type Bindings…. ● An explicit declaration is a statement in a program that lists variable names and specifies that they are a particular type. ● An implicit declaration is a means of associating variables with types through default conventions, rather than declaration statements. In this case, the first appearance of a variable name in a program constitutes its implicit declaration. ● Both explicit and implicit declarations create static bindings to types.
  • 25. Type Bindings…. Dynamic Type Binding ● With dynamic type binding, the type of a variable is not specified by a declaration statement, nor can it be determined by the spelling of its name. Instead, the variable is bound to a type when it is assigned a value in an assignment statement. ● When the assignment statement is executed, the variable being assigned is bound to the type of the value of the expression on the right side of the nassignment. ● Such an assignment may also bind the variable to an address and a memory cell, because different type values may require different amounts of storage.
  • 26. Storage Bindings and Lifetime ● Allocation: The memory cell to which a variable is bound somehow must be taken from a pool of available memory. ● Deallocation is the process of placing a memory cell that has been unbound from a variable back into the pool of available memory. ● The lifetime of a variable is the time during which the variable is bound to a specific memory location. ● So, the lifetime of a variable begins when it is bound to a specific cell and ends when it is unbound from that cell.
  • 27. Static variables ● Static variables are those that are bound to memory cells before program execution begins and remain bound to those same memory cells until program execution terminates.
  • 28. Local and Global Scope ● Local Scope: program structure that is a sequence of function definitions, in which variable definitions can appear inside the functions. ● Global scope Some languages, including C, C++, PHP, JavaScript, and Python, allow a program structure that is a sequence of function definitions, in which variable definitions can appear outside the functions. Definitions outside functions in a file create global variables, which potentially can be visible to those functions.
  • 29. Scope and Lifetime ● Sometimes the scope and lifetime of a variable appear to be related.
  • 30. Referencing Environments ● The referencing environment of a statement is the collection of all variables ● that are visible in the statement. ● The referencing environment of a statement in a static-scoped language is the variables declared in its local scope plus the collection of all variables of its ancestor scopes that are visible. ● In such a language, the referencing environment of a statement is needed while that statement is being compiled, so code and data structures can be created to allow references to variables from other scopes during run time.
  • 31. Named constant ● A named constant is a variable that is bound to a value only once. ● Named constants are useful as aids to readability and program reliability. ● Readability can be improved, for example, by using the name pi instead of the constant 3.14159265.
  • 33. END