SlideShare a Scribd company logo
Concept of Scoping in
Programming Languages
Mohammed Jafar Sadik
ID-1620660042
Dept. of ECE, NSU
What Is Scoping?
▪ Scope refers to the visibility of variables. In other words, which parts
of your program can see or use it. Normally, every variable has a
global scope. Once defined, every part of your program can access
the variable.
Why Scoping?
▪ In General, Scoping is necessary if you want to reuse variable names
▪ Example:
int fn1()
{
int a =10;
}
int fn2()
{
int a=20;
} //We are using same variable in two different functions.
Why Scoping? (Contd.)
▪ Scoping rules are crucial in modular programming, so a change in one part of program
does not break an unrelated part.
▪ Example: Imagine you are a project manager in a software company, you have 10
developers working on your project.You divided the work in 10 parts, let’s assume they
are working on 10 different Java classes.They are defining the variables by their own.
They don’t even know if the variable they are defining, are being defined or has been
defined by other developers in other classes too.They shouldn’t even have to worry
about that too. Because the SCOPING of the variables are helping that they’ll work
only in their class and will not effect other or upper tier functionalities.
▪HOW IS IT POSSIBLE???
Why Scoping (contd.)
▪ A class defined by developer1:
public class Dog
{ String breed, int age, String
color;
void barking() { // }
void hungry() { // }
void sleeping() { // }
}
▪ A class defined by developer2:
public class Cat
{ String breed, int age, String
color;
void meaowing() { // }
void hungry() { // }
void sleeping() { // }
}
**Here two different developers defining two different classes with same variable
names.They shouldn’t be worried about this.Though they are working on the
same project but their variable scope is limited within their own class.
Types of Scoping…
▪ There are two types of Scoping:
1. Static Scoping or Lexical Scoping
2. Dynamic Scoping
Static or Lexical Scoping
▪ In Static scoping, definition of a variable is resolved by searching its containing block or function. If
that fails, then searching the outer containing block and so on. Let’s consider an example:
int a=10, b=20;
int fnc()
{ int a =5;
if(condition)
{
int c;
c=b/a;
printf(“%d”,c);
}
} //OUTPUT: 20/5=4
Static scoping in depth…
int fun1(int);
int fun2(int);
int a=5;
int main()
{
int a =10;
a=fun1(a);//calling the fun1, so we will jump into it
printf(“%d”, a);
}
int fun1(int b) //b=10
{
b=b+10; //b=20 here
b=fun2(b) //calling the fun2, so we will jump into it.
return b;
}
int fun2(int b) //b=20
{
int c;
c= a+ b; /*b=20 here, but no values for a, so we will go to the
global variable a*/
return c; //5+20=25, return 25
}
Static scoping in depth…
int fun1(int);
int fun2(int);
int a=5;
int main()
{
int a =10;
a=fun1(a);//calling the fun1, so we will jump into it
printf(“%d”, a);
}
int fun1(int b) //b=10
{
b=b+10; //b=20 here
b=fun2(b) //calling the fun2, so we will jump into it.
return b;
}
int fun2(int b) //b=20
{
int c;
c= a+ b; /*b=20 here, but no values for a, so we will go to the
global variable a*/
return c; //5+20=25, return 25
}
//b=25
now
Static scoping in depth…
int fun1(int);
int fun2(int);
int a=5;
int main()
{
int a =10;
a=fun1(a);//calling the fun1, so we will jump into it
printf(“%d”, a);
}
int fun1(int b) //b=10
{
b=b+10; //b=20 here
b=fun2(b) //calling the fun2, so we will jump into it.
return b;
}
int fun2(int b) //b=20
{
int c;
c= a+ b; /*b=20 here, but no values for a, so we will go to the
global variable a*/
return c; //5+20=25, return 25
}
//b=25
now
//a=25
Static scoping in depth…
int fun1(int);
int fun2(int);
int a=5;
int main()
{
int a =10;
a=fun1(a);//calling the fun1, so we will jump into it
printf(“%d”, a);
}
int fun1(int b) //b=10
{
b=b+10; //b=20 here
b=fun2(b) //calling the fun2, so we will jump into it.
return b;
}
int fun2(int b) //b=20
{
int c;
c= a+ b; /*b=20 here, but no values for a, so we will go to the
global variable a*/
return c; //5+20=25, return 25
}
//b=25
now
//a=25
Final output:25
▪ In dynamic scoping, definition of a variable is resolved by searching its containing block and if not found, then
searching its calling function and if still not found then the function which called that calling function will be
searched and so on…
int x=5;
int fn1() int fn3()
{ int x =10; {
fn2(); fn4(); will search for variable x in fn4(), not found.
} } will search for variable x in the function that
int fn2() int fn4() called the fn4(), which is fn3(), no x in fn3(),
will search for x in the function that called fn3(),
{ { which is fn2(), not found, will search for x in the
fn3(); printf(“%d”, x); function which called fn2(), which is fn1(), in fn1()
} } we have variable x=10, output=10; but in case of
static scoping the output would be 5
Dynamic scoping…
Dynamic scoping in depth…
int fun1(int);
int fun2(int);
int a=5;
int main()
{
int a =10;
a=fun1(a);//calling the fun1, so we will jump into it
printf(“%d”, a);
}
int fun1(int b) //b=10
{
b=b+10; //b=20 here
b=fun2(b) //calling the fun2, so we will jump into it.
return b;
}
int fun2(int b) //b=20
{
int c;
c= a+ b; /*b=20 here, but no values for a, so we will go to
the calling function of fun2(), which is fun1(), no variable
named a, now will go to the calling function of fun1(), which
is main(). main() has the variable a, which is = 10*/
return c; //10+20=30, return 30
}
Dynamic scoping in depth…
int fun1(int);
int fun2(int);
int a=5;
int main()
{
int a =10;
a=fun1(a);//calling the fun1, so we will jump into it
printf(“%d”, a);
}
int fun1(int b) //b=10
{
b=b+10; //b=20 here
b=fun2(b) //calling the fun2, so we will jump into it.
return b;
}
int fun2(int b) //b=20
{
int c;
c= a+ b; /*b=20 here, but no values for a, so we will go to
the calling function of fun2(), which is fun1(), no variable
named a, now will go to the calling function of fun1(), which
is main(). main() has the variable a, which is = 10*/
return c; //10+20=30, return 30
}
//b=30
now
Dynamic scoping in depth…
int fun1(int);
int fun2(int);
int a=5;
int main()
{
int a =10;
a=fun1(a);//calling the fun1, so we will jump into it
printf(“%d”, a);
}
int fun1(int b) //b=10
{
b=b+10; //b=20 here
b=fun2(b) //calling the fun2, so we will jump into it.
return b;
}
int fun2(int b) //b=20
{
int c;
c= a+ b; /*b=20 here, but no values for a, so we will go to
the calling function of fun2(), which is fun1(), no variable
named a, now will go to the calling function of fun1(), which
is main(). main() has the variable a, which is = 10*/
return c; //10+20=30, return 30
}
//b=30
now
//a=30
Dynamic scoping in depth…
int fun1(int);
int fun2(int);
int a=5;
int main()
{
int a =10;
a=fun1(a);//calling the fun1, so we will jump into it
printf(“%d”, a);
}
int fun1(int b) //b=10
{
b=b+10; //b=20 here
b=fun2(b) //calling the fun2, so we will jump into it.
return b;
}
int fun2(int b) //b=20
{
int c;
c= a+ b; /*b=20 here, but no values for a, so we will go to
the calling function of fun2(), which is fun1(), no variable
named a, now will go to the calling function of fun1(), which
is main(). main() has the variable a, which is = 10*/
return c; //10+20=30, return 30
}
//b=30
now
//a=30
output:30
Concept of scoping in programming languages

More Related Content

What's hot

Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5
Ismar Silveira
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6rohassanie
 
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1Little Tukta Lita
 
C aptitude scribd
C aptitude scribdC aptitude scribd
C aptitude scribdAmit Kapoor
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
Ismar Silveira
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
ssuserd6b1fd
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3rohassanie
 
Mcq ppt Php- array
Mcq ppt Php- array Mcq ppt Php- array
Mcq ppt Php- array
Shaheen Shaikh
 
Lecture 11 - Functions
Lecture 11 - FunctionsLecture 11 - Functions
Lecture 11 - Functions
Md. Imran Hossain Showrov
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c language
MomenMostafa
 
Lecture 14 - Scope Rules
Lecture 14 - Scope RulesLecture 14 - Scope Rules
Lecture 14 - Scope Rules
Md. Imran Hossain Showrov
 
Preprocessor Programming
Preprocessor ProgrammingPreprocessor Programming
Preprocessor Programming
lactrious
 
Kuliah komputer pemrograman
Kuliah  komputer pemrogramanKuliah  komputer pemrograman
Kuliah komputer pemrogramanhardryu
 
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1Little Tukta Lita
 
Lecture 13 - Storage Classes
Lecture 13 - Storage ClassesLecture 13 - Storage Classes
Lecture 13 - Storage Classes
Md. Imran Hossain Showrov
 
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
corehard_by
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
Tushar B Kute
 

What's hot (20)

Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
 
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
 
C aptitude scribd
C aptitude scribdC aptitude scribd
C aptitude scribd
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
 
functions
functionsfunctions
functions
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
 
Mcq ppt Php- array
Mcq ppt Php- array Mcq ppt Php- array
Mcq ppt Php- array
 
pointers 1
pointers 1pointers 1
pointers 1
 
Lecture 11 - Functions
Lecture 11 - FunctionsLecture 11 - Functions
Lecture 11 - Functions
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c language
 
Lecture 14 - Scope Rules
Lecture 14 - Scope RulesLecture 14 - Scope Rules
Lecture 14 - Scope Rules
 
Preprocessor Programming
Preprocessor ProgrammingPreprocessor Programming
Preprocessor Programming
 
Kuliah komputer pemrograman
Kuliah  komputer pemrogramanKuliah  komputer pemrograman
Kuliah komputer pemrograman
 
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
 
Lecture 13 - Storage Classes
Lecture 13 - Storage ClassesLecture 13 - Storage Classes
Lecture 13 - Storage Classes
 
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 

Similar to Concept of scoping in programming languages

01 Introduction to Kotlin - Programming in Kotlin.pptx
01 Introduction to Kotlin - Programming in Kotlin.pptx01 Introduction to Kotlin - Programming in Kotlin.pptx
01 Introduction to Kotlin - Programming in Kotlin.pptx
IvanZawPhyo
 
Introduction to Kotlin.pptx
Introduction to Kotlin.pptxIntroduction to Kotlin.pptx
Introduction to Kotlin.pptx
AzharFauzan9
 
The following is the (incomplete) header file for the class Fracti.pdf
The following is the (incomplete) header file for the class Fracti.pdfThe following is the (incomplete) header file for the class Fracti.pdf
The following is the (incomplete) header file for the class Fracti.pdf
4babies2010
 
Functional Programming in PHP
Functional Programming in PHPFunctional Programming in PHP
Functional Programming in PHP
pwmosquito
 
Lies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerLies Told By The Kotlin Compiler
Lies Told By The Kotlin Compiler
Garth Gilmour
 
Functions
FunctionsFunctions
Functions
Swarup Boro
 
EcmaScript unchained
EcmaScript unchainedEcmaScript unchained
EcmaScript unchained
Eduard Tomàs
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Mario Fusco
 
C programming
C programmingC programming
C programming
Samsil Arefin
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
josies1
 
Function recap
Function recapFunction recap
Function recapalish sha
 
Function recap
Function recapFunction recap
Function recapalish sha
 
EcmaScript 6
EcmaScript 6 EcmaScript 6
EcmaScript 6
Manoj Kumar
 
Golang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / OverviewGolang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / Overview
Markus Schneider
 
PROGRAM 2 – Fraction Class Problem For this programming as.pdf
PROGRAM 2 – Fraction Class Problem For this programming as.pdfPROGRAM 2 – Fraction Class Problem For this programming as.pdf
PROGRAM 2 – Fraction Class Problem For this programming as.pdf
ezonesolutions
 
Benginning Calculus Lecture notes 1 - functions
Benginning Calculus Lecture notes 1 - functionsBenginning Calculus Lecture notes 1 - functions
Benginning Calculus Lecture notes 1 - functions
basyirstar
 
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
RashidFaridChishti
 
Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)
hhliu
 
Advanced JavaScript
Advanced JavaScript Advanced JavaScript
Advanced JavaScript
Zsolt Mészárovics
 

Similar to Concept of scoping in programming languages (20)

01 Introduction to Kotlin - Programming in Kotlin.pptx
01 Introduction to Kotlin - Programming in Kotlin.pptx01 Introduction to Kotlin - Programming in Kotlin.pptx
01 Introduction to Kotlin - Programming in Kotlin.pptx
 
Introduction to Kotlin.pptx
Introduction to Kotlin.pptxIntroduction to Kotlin.pptx
Introduction to Kotlin.pptx
 
The following is the (incomplete) header file for the class Fracti.pdf
The following is the (incomplete) header file for the class Fracti.pdfThe following is the (incomplete) header file for the class Fracti.pdf
The following is the (incomplete) header file for the class Fracti.pdf
 
Functional Programming in PHP
Functional Programming in PHPFunctional Programming in PHP
Functional Programming in PHP
 
Lies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerLies Told By The Kotlin Compiler
Lies Told By The Kotlin Compiler
 
Functions
FunctionsFunctions
Functions
 
EcmaScript unchained
EcmaScript unchainedEcmaScript unchained
EcmaScript unchained
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
C programming
C programmingC programming
C programming
 
6. function
6. function6. function
6. function
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
 
Function recap
Function recapFunction recap
Function recap
 
Function recap
Function recapFunction recap
Function recap
 
EcmaScript 6
EcmaScript 6 EcmaScript 6
EcmaScript 6
 
Golang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / OverviewGolang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / Overview
 
PROGRAM 2 – Fraction Class Problem For this programming as.pdf
PROGRAM 2 – Fraction Class Problem For this programming as.pdfPROGRAM 2 – Fraction Class Problem For this programming as.pdf
PROGRAM 2 – Fraction Class Problem For this programming as.pdf
 
Benginning Calculus Lecture notes 1 - functions
Benginning Calculus Lecture notes 1 - functionsBenginning Calculus Lecture notes 1 - functions
Benginning Calculus Lecture notes 1 - functions
 
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
 
Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)
 
Advanced JavaScript
Advanced JavaScript Advanced JavaScript
Advanced JavaScript
 

Recently uploaded

Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
timtebeek1
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
Rakesh Kumar R
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
Deuglo Infosystem Pvt Ltd
 
Launch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in MinutesLaunch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in Minutes
Roshan Dwivedi
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
Boni García
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Łukasz Chruściel
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Crescat
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
Google
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Aftab Hussain
 

Recently uploaded (20)

Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
 
Launch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in MinutesLaunch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in Minutes
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
 

Concept of scoping in programming languages

  • 1. Concept of Scoping in Programming Languages Mohammed Jafar Sadik ID-1620660042 Dept. of ECE, NSU
  • 2. What Is Scoping? ▪ Scope refers to the visibility of variables. In other words, which parts of your program can see or use it. Normally, every variable has a global scope. Once defined, every part of your program can access the variable.
  • 3. Why Scoping? ▪ In General, Scoping is necessary if you want to reuse variable names ▪ Example: int fn1() { int a =10; } int fn2() { int a=20; } //We are using same variable in two different functions.
  • 4. Why Scoping? (Contd.) ▪ Scoping rules are crucial in modular programming, so a change in one part of program does not break an unrelated part. ▪ Example: Imagine you are a project manager in a software company, you have 10 developers working on your project.You divided the work in 10 parts, let’s assume they are working on 10 different Java classes.They are defining the variables by their own. They don’t even know if the variable they are defining, are being defined or has been defined by other developers in other classes too.They shouldn’t even have to worry about that too. Because the SCOPING of the variables are helping that they’ll work only in their class and will not effect other or upper tier functionalities. ▪HOW IS IT POSSIBLE???
  • 5. Why Scoping (contd.) ▪ A class defined by developer1: public class Dog { String breed, int age, String color; void barking() { // } void hungry() { // } void sleeping() { // } } ▪ A class defined by developer2: public class Cat { String breed, int age, String color; void meaowing() { // } void hungry() { // } void sleeping() { // } } **Here two different developers defining two different classes with same variable names.They shouldn’t be worried about this.Though they are working on the same project but their variable scope is limited within their own class.
  • 6. Types of Scoping… ▪ There are two types of Scoping: 1. Static Scoping or Lexical Scoping 2. Dynamic Scoping
  • 7. Static or Lexical Scoping ▪ In Static scoping, definition of a variable is resolved by searching its containing block or function. If that fails, then searching the outer containing block and so on. Let’s consider an example: int a=10, b=20; int fnc() { int a =5; if(condition) { int c; c=b/a; printf(“%d”,c); } } //OUTPUT: 20/5=4
  • 8. Static scoping in depth… int fun1(int); int fun2(int); int a=5; int main() { int a =10; a=fun1(a);//calling the fun1, so we will jump into it printf(“%d”, a); } int fun1(int b) //b=10 { b=b+10; //b=20 here b=fun2(b) //calling the fun2, so we will jump into it. return b; } int fun2(int b) //b=20 { int c; c= a+ b; /*b=20 here, but no values for a, so we will go to the global variable a*/ return c; //5+20=25, return 25 }
  • 9. Static scoping in depth… int fun1(int); int fun2(int); int a=5; int main() { int a =10; a=fun1(a);//calling the fun1, so we will jump into it printf(“%d”, a); } int fun1(int b) //b=10 { b=b+10; //b=20 here b=fun2(b) //calling the fun2, so we will jump into it. return b; } int fun2(int b) //b=20 { int c; c= a+ b; /*b=20 here, but no values for a, so we will go to the global variable a*/ return c; //5+20=25, return 25 } //b=25 now
  • 10. Static scoping in depth… int fun1(int); int fun2(int); int a=5; int main() { int a =10; a=fun1(a);//calling the fun1, so we will jump into it printf(“%d”, a); } int fun1(int b) //b=10 { b=b+10; //b=20 here b=fun2(b) //calling the fun2, so we will jump into it. return b; } int fun2(int b) //b=20 { int c; c= a+ b; /*b=20 here, but no values for a, so we will go to the global variable a*/ return c; //5+20=25, return 25 } //b=25 now //a=25
  • 11. Static scoping in depth… int fun1(int); int fun2(int); int a=5; int main() { int a =10; a=fun1(a);//calling the fun1, so we will jump into it printf(“%d”, a); } int fun1(int b) //b=10 { b=b+10; //b=20 here b=fun2(b) //calling the fun2, so we will jump into it. return b; } int fun2(int b) //b=20 { int c; c= a+ b; /*b=20 here, but no values for a, so we will go to the global variable a*/ return c; //5+20=25, return 25 } //b=25 now //a=25 Final output:25
  • 12. ▪ In dynamic scoping, definition of a variable is resolved by searching its containing block and if not found, then searching its calling function and if still not found then the function which called that calling function will be searched and so on… int x=5; int fn1() int fn3() { int x =10; { fn2(); fn4(); will search for variable x in fn4(), not found. } } will search for variable x in the function that int fn2() int fn4() called the fn4(), which is fn3(), no x in fn3(), will search for x in the function that called fn3(), { { which is fn2(), not found, will search for x in the fn3(); printf(“%d”, x); function which called fn2(), which is fn1(), in fn1() } } we have variable x=10, output=10; but in case of static scoping the output would be 5 Dynamic scoping…
  • 13. Dynamic scoping in depth… int fun1(int); int fun2(int); int a=5; int main() { int a =10; a=fun1(a);//calling the fun1, so we will jump into it printf(“%d”, a); } int fun1(int b) //b=10 { b=b+10; //b=20 here b=fun2(b) //calling the fun2, so we will jump into it. return b; } int fun2(int b) //b=20 { int c; c= a+ b; /*b=20 here, but no values for a, so we will go to the calling function of fun2(), which is fun1(), no variable named a, now will go to the calling function of fun1(), which is main(). main() has the variable a, which is = 10*/ return c; //10+20=30, return 30 }
  • 14. Dynamic scoping in depth… int fun1(int); int fun2(int); int a=5; int main() { int a =10; a=fun1(a);//calling the fun1, so we will jump into it printf(“%d”, a); } int fun1(int b) //b=10 { b=b+10; //b=20 here b=fun2(b) //calling the fun2, so we will jump into it. return b; } int fun2(int b) //b=20 { int c; c= a+ b; /*b=20 here, but no values for a, so we will go to the calling function of fun2(), which is fun1(), no variable named a, now will go to the calling function of fun1(), which is main(). main() has the variable a, which is = 10*/ return c; //10+20=30, return 30 } //b=30 now
  • 15. Dynamic scoping in depth… int fun1(int); int fun2(int); int a=5; int main() { int a =10; a=fun1(a);//calling the fun1, so we will jump into it printf(“%d”, a); } int fun1(int b) //b=10 { b=b+10; //b=20 here b=fun2(b) //calling the fun2, so we will jump into it. return b; } int fun2(int b) //b=20 { int c; c= a+ b; /*b=20 here, but no values for a, so we will go to the calling function of fun2(), which is fun1(), no variable named a, now will go to the calling function of fun1(), which is main(). main() has the variable a, which is = 10*/ return c; //10+20=30, return 30 } //b=30 now //a=30
  • 16. Dynamic scoping in depth… int fun1(int); int fun2(int); int a=5; int main() { int a =10; a=fun1(a);//calling the fun1, so we will jump into it printf(“%d”, a); } int fun1(int b) //b=10 { b=b+10; //b=20 here b=fun2(b) //calling the fun2, so we will jump into it. return b; } int fun2(int b) //b=20 { int c; c= a+ b; /*b=20 here, but no values for a, so we will go to the calling function of fun2(), which is fun1(), no variable named a, now will go to the calling function of fun1(), which is main(). main() has the variable a, which is = 10*/ return c; //10+20=30, return 30 } //b=30 now //a=30 output:30