SlideShare a Scribd company logo
1 of 17
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 #5Ismar 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 #4Ismar 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
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c languageMomenMostafa
 
Preprocessor Programming
Preprocessor ProgrammingPreprocessor Programming
Preprocessor Programminglactrious
 
Kuliah komputer pemrograman
Kuliah  komputer pemrogramanKuliah  komputer pemrograman
Kuliah komputer pemrogramanhardryu
 
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1Little Tukta Lita
 
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019corehard_by
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of cTushar 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

Introduction to Kotlin.pptx
Introduction to Kotlin.pptxIntroduction to Kotlin.pptx
Introduction to Kotlin.pptxAzharFauzan9
 
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.pptxIvanZawPhyo
 
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.pdf4babies2010
 
Functional Programming in PHP
Functional Programming in PHPFunctional Programming in PHP
Functional Programming in PHPpwmosquito
 
Lies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerLies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerGarth Gilmour
 
EcmaScript unchained
EcmaScript unchainedEcmaScript unchained
EcmaScript unchainedEduard 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
 
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.docxjosies1
 
Function recap
Function recapFunction recap
Function recapalish sha
 
Function recap
Function recapFunction recap
Function recapalish sha
 
Golang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / OverviewGolang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / OverviewMarkus 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.pdfezonesolutions
 
Benginning Calculus Lecture notes 1 - functions
Benginning Calculus Lecture notes 1 - functionsBenginning Calculus Lecture notes 1 - functions
Benginning Calculus Lecture notes 1 - functionsbasyirstar
 
Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)hhliu
 
Something about Golang
Something about GolangSomething about Golang
Something about GolangAnton Arhipov
 

Similar to Concept of scoping in programming languages (20)

Introduction to Kotlin.pptx
Introduction to Kotlin.pptxIntroduction to Kotlin.pptx
Introduction to Kotlin.pptx
 
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
 
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
 
Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)
 
Advanced JavaScript
Advanced JavaScript Advanced JavaScript
Advanced JavaScript
 
Something about Golang
Something about GolangSomething about Golang
Something about Golang
 

Recently uploaded

The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Intelisync
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 

Recently uploaded (20)

The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 

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