SlideShare a Scribd company logo
1 of 24
CHAPTER 3
SCOPING
Learning Objectives
• Understand what is Scoping
• Able to implement the LEGB rule
• Understand what is module
• Understand the implementation of
access control in programming language
Introduction
Scope refers to the visibility of
variables, parameters and functions in
one part of a program to another part of
the same program.
Variable Scope
The process of binding a
variable name with an object is
calledmapping. : = (colon equal to
sign) isused in programming
languages tomap the variable and
object.
Programming languages keeps track of all these mappings
with namespaces.
Namespaces are containers for mapping names of
variables to objects.
In the following example, a is fi rst mapped to the integer 5.
In this case, a is the variable name, while the integer value 5
is the object.
Th en, b is set equal to a. Th is actuallymeans that b is now
bound to the same
integer value as a, which is 5.
1. a:=5
2. b:=a
LEGB rule
Scope also defines the order in
which variables have to be
mapped to the object in order to
obtain the value. Let us take a
simple example as shown below:
1. x:= 'outer x variable'
2. display():
3. x:= 'inner x variable'
4. print x
5. display()
When the above statements are executed
the
statement (4) and (5) display the result as
Output
outer x variable
inner x variable
The LEGB rule is used to decide the
order in which the scopes are to be
searched for scope resolution. The
scopes are listed below in terms of
hierarchy (highest to lowest).
Types of
Variable
Scope
There are 4 types of Variable
Scope,
LOCAL SCOPE
GLOBAL SCOPE
ENCLOSED SCOPE
BUILT IN SCOPE
Local Scope
Local scope refers to variables
defined in current function. Always,
a function will first look up for a
variable name in its local scope. Only
if it does not find it there, the outer
scopes are checked.
Global Scope
variable which is declared outside of all
the functions in a program is known as
global
variable. This means, global variable can
be accessed inside or outside of all the
functions in a program. Consider the
following example
Enclosed Scope
All programming languages permit functions to be
nested. A function (method) with in another function is
function is called nested function. A variable which is
which is declared inside a function which contains
contains another function definition with in it, the
inner function can also access the variable of the
outer function. This scope is called enclosed scope.
Built-in Scope
Finally, we discuss about the widest scope. The
built-in scope has all the names that are pre-loaded
into the program scope when we start the compiler
or interpreter. Any variable or module which is
defined in the library functions of a programming
language has Built-in or module scope. They are
loaded as soon as the library files are imported to
the program
Module
A module is a part of a program. Programs are
composed of one or more independently developed
modules. A single module can contain one or several
other. Modules work perfectly on individual level and
can be integrated with other modules. A software
program can be divided into modules to ease the job
of programming and debugging as well. A program
can be divided into small functional modules that
work together to get the output. The process of
subdividing a computer program intose parate sub-
programs is called Modular programming. The
examples of modules are procedures, subroutines,
and functions.
Characteristics of
Modules
1. Modules contain instructions, processing logic,
and data.
2. Modules can be separately compiled and stored in
a library.
3. Modules can be included in a program.
4. Module segments can be used by invoking a
name and some parameters.
5. Module segments can be used by other modules.
The benefits of using modular programming include
Less code to be written.
• A single procedure can be developed for reuse, eliminating the
need to retype the code many times.
• Programs can be designed more easily because a small team deals
with only a small part of the entire code.
• Modular programming allows many programmers to collaborate on
the same application.
• The code is stored across multiple files.
• Code is short, simple and easy to understand.
• Errors can easily be identified, as they are localized to a subroutine
or function.
• The same code can be used in many applications.
• The scoping of variables can easily be controlled.
Access Control
Access control is a security
technique that regulates who or
what can view or use resources in a
computing environment.
Private members of a class are denied access from the outside the
class. They can be handled only from within the class. Public
members (generally methods declared in a class) are accessible
from outside the class. The object of the same class is required to
invoke a public method.
This arrangement of private instance variables and public methods
methods ensures the principle of data encapsulation.
Protected members of a class are accessible from within the class
and are also available to its sub-classes. No other process is
permitted access to it. This enables specific resources of the
parent.All members in a Python class are public by default,
whereas by default in C++ and java they are private.

More Related Content

Similar to Chapter-3 Scoping.pptx

DOC-20210303-WA0017..pptx,coding stuff in c
DOC-20210303-WA0017..pptx,coding stuff in cDOC-20210303-WA0017..pptx,coding stuff in c
DOC-20210303-WA0017..pptx,coding stuff in cfloraaluoch3
 
The Java Learning Kit Chapter 5 – Methods and Modular.docx
The Java Learning Kit Chapter 5 – Methods and Modular.docxThe Java Learning Kit Chapter 5 – Methods and Modular.docx
The Java Learning Kit Chapter 5 – Methods and Modular.docxarnoldmeredith47041
 
Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using ReflectionGanesh Samarthyam
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptAmanuelZewdie4
 
12th ip CBSE chapter 4 oop in java notes complete
12th ip CBSE  chapter 4 oop in java notes complete12th ip CBSE  chapter 4 oop in java notes complete
12th ip CBSE chapter 4 oop in java notes completeHarish Gyanani
 
Password protected diary
Password protected diaryPassword protected diary
Password protected diarySHARDA SHARAN
 
java tutorial for beginners learning.ppt
java tutorial for beginners learning.pptjava tutorial for beginners learning.ppt
java tutorial for beginners learning.pptusha852
 
Oops abap fundamental
Oops abap fundamentalOops abap fundamental
Oops abap fundamentalbiswajit2015
 
PCCF-UNIT 2-1 new.docx
PCCF-UNIT 2-1 new.docxPCCF-UNIT 2-1 new.docx
PCCF-UNIT 2-1 new.docxprakashvs7
 
oopsinvb-191021101327.pdf
oopsinvb-191021101327.pdfoopsinvb-191021101327.pdf
oopsinvb-191021101327.pdfJP Chicano
 
Oosd lecture unit 4 ppt introduction part
Oosd lecture unit 4 ppt introduction partOosd lecture unit 4 ppt introduction part
Oosd lecture unit 4 ppt introduction partManuSingh669370
 
Object Oriented Programming Overview for the PeopleSoft Developer
Object Oriented Programming Overview for the PeopleSoft DeveloperObject Oriented Programming Overview for the PeopleSoft Developer
Object Oriented Programming Overview for the PeopleSoft DeveloperLee Greffin
 
PCCF UNIT 2.pptx
PCCF UNIT 2.pptxPCCF UNIT 2.pptx
PCCF UNIT 2.pptxDivyaKS12
 

Similar to Chapter-3 Scoping.pptx (20)

DOC-20210303-WA0017..pptx,coding stuff in c
DOC-20210303-WA0017..pptx,coding stuff in cDOC-20210303-WA0017..pptx,coding stuff in c
DOC-20210303-WA0017..pptx,coding stuff in c
 
The Java Learning Kit Chapter 5 – Methods and Modular.docx
The Java Learning Kit Chapter 5 – Methods and Modular.docxThe Java Learning Kit Chapter 5 – Methods and Modular.docx
The Java Learning Kit Chapter 5 – Methods and Modular.docx
 
Oop basic overview
Oop basic overviewOop basic overview
Oop basic overview
 
Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using Reflection
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.ppt
 
The smartpath information systems c plus plus
The smartpath information systems  c plus plusThe smartpath information systems  c plus plus
The smartpath information systems c plus plus
 
12th ip CBSE chapter 4 oop in java notes complete
12th ip CBSE  chapter 4 oop in java notes complete12th ip CBSE  chapter 4 oop in java notes complete
12th ip CBSE chapter 4 oop in java notes complete
 
Introduction to problem solving in C
Introduction to problem solving in CIntroduction to problem solving in C
Introduction to problem solving in C
 
OOPJ.pptx
OOPJ.pptxOOPJ.pptx
OOPJ.pptx
 
Password protected diary
Password protected diaryPassword protected diary
Password protected diary
 
java tutorial for beginners learning.ppt
java tutorial for beginners learning.pptjava tutorial for beginners learning.ppt
java tutorial for beginners learning.ppt
 
Oops abap fundamental
Oops abap fundamentalOops abap fundamental
Oops abap fundamental
 
Training 8051Report
Training 8051ReportTraining 8051Report
Training 8051Report
 
PCCF-UNIT 2-1 new.docx
PCCF-UNIT 2-1 new.docxPCCF-UNIT 2-1 new.docx
PCCF-UNIT 2-1 new.docx
 
oopsinvb-191021101327.pdf
oopsinvb-191021101327.pdfoopsinvb-191021101327.pdf
oopsinvb-191021101327.pdf
 
Oops in vb
Oops in vbOops in vb
Oops in vb
 
Oosd lecture unit 4 ppt introduction part
Oosd lecture unit 4 ppt introduction partOosd lecture unit 4 ppt introduction part
Oosd lecture unit 4 ppt introduction part
 
Object Oriented Programming Overview for the PeopleSoft Developer
Object Oriented Programming Overview for the PeopleSoft DeveloperObject Oriented Programming Overview for the PeopleSoft Developer
Object Oriented Programming Overview for the PeopleSoft Developer
 
Functions
FunctionsFunctions
Functions
 
PCCF UNIT 2.pptx
PCCF UNIT 2.pptxPCCF UNIT 2.pptx
PCCF UNIT 2.pptx
 

Recently uploaded

Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
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
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
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
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
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
 

Recently uploaded (20)

Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
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
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
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🔝
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
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
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
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
 

Chapter-3 Scoping.pptx

  • 2. Learning Objectives • Understand what is Scoping • Able to implement the LEGB rule • Understand what is module • Understand the implementation of access control in programming language
  • 3. Introduction Scope refers to the visibility of variables, parameters and functions in one part of a program to another part of the same program.
  • 4. Variable Scope The process of binding a variable name with an object is calledmapping. : = (colon equal to sign) isused in programming languages tomap the variable and object.
  • 5. Programming languages keeps track of all these mappings with namespaces. Namespaces are containers for mapping names of variables to objects. In the following example, a is fi rst mapped to the integer 5. In this case, a is the variable name, while the integer value 5 is the object. Th en, b is set equal to a. Th is actuallymeans that b is now bound to the same integer value as a, which is 5. 1. a:=5 2. b:=a
  • 6.
  • 7. LEGB rule Scope also defines the order in which variables have to be mapped to the object in order to obtain the value. Let us take a simple example as shown below:
  • 8. 1. x:= 'outer x variable' 2. display(): 3. x:= 'inner x variable' 4. print x 5. display() When the above statements are executed the statement (4) and (5) display the result as Output outer x variable inner x variable
  • 9. The LEGB rule is used to decide the order in which the scopes are to be searched for scope resolution. The scopes are listed below in terms of hierarchy (highest to lowest).
  • 10.
  • 11. Types of Variable Scope There are 4 types of Variable Scope, LOCAL SCOPE GLOBAL SCOPE ENCLOSED SCOPE BUILT IN SCOPE
  • 12. Local Scope Local scope refers to variables defined in current function. Always, a function will first look up for a variable name in its local scope. Only if it does not find it there, the outer scopes are checked.
  • 13.
  • 14. Global Scope variable which is declared outside of all the functions in a program is known as global variable. This means, global variable can be accessed inside or outside of all the functions in a program. Consider the following example
  • 15.
  • 16. Enclosed Scope All programming languages permit functions to be nested. A function (method) with in another function is function is called nested function. A variable which is which is declared inside a function which contains contains another function definition with in it, the inner function can also access the variable of the outer function. This scope is called enclosed scope.
  • 17.
  • 18. Built-in Scope Finally, we discuss about the widest scope. The built-in scope has all the names that are pre-loaded into the program scope when we start the compiler or interpreter. Any variable or module which is defined in the library functions of a programming language has Built-in or module scope. They are loaded as soon as the library files are imported to the program
  • 19.
  • 20. Module A module is a part of a program. Programs are composed of one or more independently developed modules. A single module can contain one or several other. Modules work perfectly on individual level and can be integrated with other modules. A software program can be divided into modules to ease the job of programming and debugging as well. A program can be divided into small functional modules that work together to get the output. The process of subdividing a computer program intose parate sub- programs is called Modular programming. The examples of modules are procedures, subroutines, and functions.
  • 21. Characteristics of Modules 1. Modules contain instructions, processing logic, and data. 2. Modules can be separately compiled and stored in a library. 3. Modules can be included in a program. 4. Module segments can be used by invoking a name and some parameters. 5. Module segments can be used by other modules.
  • 22. The benefits of using modular programming include Less code to be written. • A single procedure can be developed for reuse, eliminating the need to retype the code many times. • Programs can be designed more easily because a small team deals with only a small part of the entire code. • Modular programming allows many programmers to collaborate on the same application. • The code is stored across multiple files. • Code is short, simple and easy to understand. • Errors can easily be identified, as they are localized to a subroutine or function. • The same code can be used in many applications. • The scoping of variables can easily be controlled.
  • 23. Access Control Access control is a security technique that regulates who or what can view or use resources in a computing environment.
  • 24. Private members of a class are denied access from the outside the class. They can be handled only from within the class. Public members (generally methods declared in a class) are accessible from outside the class. The object of the same class is required to invoke a public method. This arrangement of private instance variables and public methods methods ensures the principle of data encapsulation. Protected members of a class are accessible from within the class and are also available to its sub-classes. No other process is permitted access to it. This enables specific resources of the parent.All members in a Python class are public by default, whereas by default in C++ and java they are private.