SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
- presentation by
Nusrat.Mohammed.Hanif
What is Scoping ?
1. Scoping refers to the visibility of variables . In simple
words , we can say a textual region of program in which
binding is active is its scope.
2. A scope is the body of module class , subroutine or
structured control flow statement sometimes called as
block .
Types of Scopes:
There are 2 types of scopes :
1.Static Scoping
2. Dynamic Scoping
• Static Scoping : it is also called as lexical scoping. It is binding between
names and object can be determined at compile time. In static scoping
the compiler checks for say variable “x” if it is present in local block if it
is not there then it search in the main or parent block.
• Dynamic Scoping : It is binding between names and object can be
determined at run time. In dynamic scoping the compiler checks for say
variable “x” if it is present in local block if it is not there then it search
in the sibling functions block or from where it is called.
 Example : 1
i n t x ;
i n t m a i n ( )
{
x = 2 ;
f ( ) ;
g ( ) ;
}
V o i d f ( )
{
i n t x = 3 ;
h ( ) ;
}
v o i d g ( )
{
i n t x = 4 ;
h ( )
v o i d h ( )
{
p r i n t f ( “ % d  ” , x ) ;
}
Output :
• By static rule :
x=2  main’s x
x=2  main’s x
• By dynamic rule :
x=3  f()’s x
x=4  g()’s x
 Example : 2
B e g i n
i n t e g e r m , n ;
P r o c e d u r e h a r d y :
b e g i n
p r i n t ( “ i n h a r d y n = “ , n ) ;
e n d ;
P r o c e d u r e l a u r e l ( n : i n t ) :
b e g i n
p r i n t ( “ i n l a u r e l m = “ ,
m ) ;
p r i n t ( “ i n l a u r e l n = “ , n ) ;
h a r d y ;
e n d ;
/ / m = 5 0 ; n = 1 0 0 ;
p r i n t ( i n m a i n p r o g n = “ , n ) ;
l a u r e l ( 1 ) ;
h a r d y ;
e n d ;
Out put :
• By st atic r ule :
n=100  main’s n
m=50  main’s m
n=1  l aurel’s n
n=100  main’s n
n=100  main’s n
• By dynamic r ule ( sibling)
n=100  main’s n
m=50  main’s m
n=1  l aurel’s n
n=1  ( hardy)laurel ’s n
n=100  main’s n
 Example : 3
i n t b = 5 ;
i n t f o o ( )
{
i n t a = b + 5 ;
r e t u r n a ;
}
i n t b a r ( )
{
i n t b = 2 ;
r e t u r n f o o ( ) ;
}
i n t m a i n ( )
{
f o o ( ) ;
b a r ( ) ;
r e t u r n 0 ;
}
Output :
• By static rule :
foo=10 (foo‘s return a)
bar =10 (foo’s return a)
• By dynamic rule :
foo=10 (foo‘s return a)
bar =7 (bar‘s return foo)

More Related Content

PPT
Time complexity
PPTX
Types of Parser
PPTX
Syntax Analysis in Compiler Design
PPTX
Type checking in compiler design
PDF
Syntax Directed Definition and its applications
PPTX
Graph coloring using backtracking
PDF
LR(1) CLR(1) Parser with Example
PPTX
Code generation
Time complexity
Types of Parser
Syntax Analysis in Compiler Design
Type checking in compiler design
Syntax Directed Definition and its applications
Graph coloring using backtracking
LR(1) CLR(1) Parser with Example
Code generation

What's hot (20)

PPTX
Input-Buffering
PPTX
Intermediate code generator
PPTX
Lecture 14 run time environment
PPTX
Lexical analyzer generator lex
PPT
Intermediate code generation (Compiler Design)
PPTX
Code Generation
PPT
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
PPTX
Recognition-of-tokens
PDF
loaders and linkers
DOC
Time and space complexity
PPTX
Problem solving in Artificial Intelligence.pptx
PPTX
Ambiguous & Unambiguous Grammar
PPTX
Ch 4 linker loader
PPTX
Dag representation of basic blocks
PPT
Lecture 1 - Lexical Analysis.ppt
PDF
Operator precedence
PPTX
System Programing Unit 1
PDF
Input-Buffering
Intermediate code generator
Lecture 14 run time environment
Lexical analyzer generator lex
Intermediate code generation (Compiler Design)
Code Generation
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
Recognition-of-tokens
loaders and linkers
Time and space complexity
Problem solving in Artificial Intelligence.pptx
Ambiguous & Unambiguous Grammar
Ch 4 linker loader
Dag representation of basic blocks
Lecture 1 - Lexical Analysis.ppt
Operator precedence
System Programing Unit 1
Ad

Similar to Static and dynamic scoping (20)

PDF
PyData Paris 2015 - Track 3.2 Serge Guelton et Pierrick Brunet
PPTX
Hardware Description Languages .pptx
PDF
Mean value, maxima minima,curve tracing
PDF
Introduction to Compiler Development
PDF
AOP via PIAB and Unity
PDF
PyLadies Talk: Learn to love the command line!
PDF
Simulating Evolution and Behaviour
PDF
lec4_annotated.pdf ml csci 567 vatsal sharan
PDF
L2-Control ppt oops (1) object_oriented.pdf
PDF
Piotr Szotkowski about "Ruby smells"
PDF
A practical Introduction to Machine(s) Learning
PPTX
Introduction to Julia
PPTX
Introduction to Asymmetric Cipher decipher
PPTX
An optimal and progressive algorithm for skyline queries slide
PDF
Code GPU with CUDA - Applying optimization techniques
PDF
Constructs and techniques and their implementation in different languages
PPT
Sample size need to be Achieved high confidence
PPTX
Class hhajjaksksbbsjajahsjjsnsnnsjksjznsmanj
PDF
Kamil witecki asynchronous, yet readable, code
PDF
02.03 Artificial Intelligence: Search by Optimization
PyData Paris 2015 - Track 3.2 Serge Guelton et Pierrick Brunet
Hardware Description Languages .pptx
Mean value, maxima minima,curve tracing
Introduction to Compiler Development
AOP via PIAB and Unity
PyLadies Talk: Learn to love the command line!
Simulating Evolution and Behaviour
lec4_annotated.pdf ml csci 567 vatsal sharan
L2-Control ppt oops (1) object_oriented.pdf
Piotr Szotkowski about "Ruby smells"
A practical Introduction to Machine(s) Learning
Introduction to Julia
Introduction to Asymmetric Cipher decipher
An optimal and progressive algorithm for skyline queries slide
Code GPU with CUDA - Applying optimization techniques
Constructs and techniques and their implementation in different languages
Sample size need to be Achieved high confidence
Class hhajjaksksbbsjajahsjjsnsnnsjksjznsmanj
Kamil witecki asynchronous, yet readable, code
02.03 Artificial Intelligence: Search by Optimization
Ad

Recently uploaded (20)

PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Digital Strategies for Manufacturing Companies
PPTX
Transform Your Business with a Software ERP System
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
System and Network Administration Chapter 2
PPTX
ai tools demonstartion for schools and inter college
PDF
AI in Product Development-omnex systems
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Operating system designcfffgfgggggggvggggggggg
Which alternative to Crystal Reports is best for small or large businesses.pdf
Digital Strategies for Manufacturing Companies
Transform Your Business with a Software ERP System
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
2025 Textile ERP Trends: SAP, Odoo & Oracle
Softaken Excel to vCard Converter Software.pdf
Odoo POS Development Services by CandidRoot Solutions
How Creative Agencies Leverage Project Management Software.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 41
How to Migrate SBCGlobal Email to Yahoo Easily
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
System and Network Administration Chapter 2
ai tools demonstartion for schools and inter college
AI in Product Development-omnex systems
PTS Company Brochure 2025 (1).pdf.......
VVF-Customer-Presentation2025-Ver1.9.pptx
Navsoft: AI-Powered Business Solutions & Custom Software Development
Adobe Illustrator 28.6 Crack My Vision of Vector Design

Static and dynamic scoping

  • 2. What is Scoping ? 1. Scoping refers to the visibility of variables . In simple words , we can say a textual region of program in which binding is active is its scope. 2. A scope is the body of module class , subroutine or structured control flow statement sometimes called as block .
  • 3. Types of Scopes: There are 2 types of scopes : 1.Static Scoping 2. Dynamic Scoping • Static Scoping : it is also called as lexical scoping. It is binding between names and object can be determined at compile time. In static scoping the compiler checks for say variable “x” if it is present in local block if it is not there then it search in the main or parent block. • Dynamic Scoping : It is binding between names and object can be determined at run time. In dynamic scoping the compiler checks for say variable “x” if it is present in local block if it is not there then it search in the sibling functions block or from where it is called.
  • 4.  Example : 1 i n t x ; i n t m a i n ( ) { x = 2 ; f ( ) ; g ( ) ; } V o i d f ( ) { i n t x = 3 ; h ( ) ; } v o i d g ( ) { i n t x = 4 ; h ( ) v o i d h ( ) { p r i n t f ( “ % d ” , x ) ; } Output : • By static rule : x=2  main’s x x=2  main’s x • By dynamic rule : x=3  f()’s x x=4  g()’s x
  • 5.  Example : 2 B e g i n i n t e g e r m , n ; P r o c e d u r e h a r d y : b e g i n p r i n t ( “ i n h a r d y n = “ , n ) ; e n d ; P r o c e d u r e l a u r e l ( n : i n t ) : b e g i n p r i n t ( “ i n l a u r e l m = “ , m ) ; p r i n t ( “ i n l a u r e l n = “ , n ) ; h a r d y ; e n d ; / / m = 5 0 ; n = 1 0 0 ; p r i n t ( i n m a i n p r o g n = “ , n ) ; l a u r e l ( 1 ) ; h a r d y ; e n d ; Out put : • By st atic r ule : n=100  main’s n m=50  main’s m n=1  l aurel’s n n=100  main’s n n=100  main’s n • By dynamic r ule ( sibling) n=100  main’s n m=50  main’s m n=1  l aurel’s n n=1  ( hardy)laurel ’s n n=100  main’s n
  • 6.  Example : 3 i n t b = 5 ; i n t f o o ( ) { i n t a = b + 5 ; r e t u r n a ; } i n t b a r ( ) { i n t b = 2 ; r e t u r n f o o ( ) ; } i n t m a i n ( ) { f o o ( ) ; b a r ( ) ; r e t u r n 0 ; } Output : • By static rule : foo=10 (foo‘s return a) bar =10 (foo’s return a) • By dynamic rule : foo=10 (foo‘s return a) bar =7 (bar‘s return foo)