SlideShare a Scribd company logo
2
Most read
3
Most read
z
Variable Scope
z
Variable Scope: Local and Global
 Scope refers to the visibility of objects. In other words which parts of
your application can see it or use it.
 Variables defined inside a function are local to that function. They are
hidden from the statements in other functions, which normally cannot
access them. When the function terminates the values are released.
 Question: What is another situation where the variable was only
known while the structure was executing?
 Because the variables defined in a function are hidden, other functions
may have separate, distinct variables with the same name.
 However, do not let this confuse you.
 Question: What does the above statement mean?
z
Local Variables
 A function’s local variables exist only while the
function is executing. This is known as the lifetime of
a local variable.
 When the function begins, its local variables and its
parameter variables are created in memory, and
when the function ends, the local variables and
parameter variables are destroyed.
 This means that any value stored in a local variable is
lost between function calls and posts.
z
Global Variables and Global Constants
 A global variable is any variable defined outside all
the functions in a program.
 The scope of a global variable is the portion of the
program from the variable definition to the end.
 This means that a global variable can be accessed by
all functions that are defined after the global variable
is defined.
Bad Programming: The use of global variables
Good Programming: Only creating global variables that
are constant, passing variables through functions
z
Updating a Global Variable
 Code:
 Updating a global variable:
 globVar = <val>
def functionName ():
global globVar
globVar = <new value>
Why: Without the keyword global inside the function referencing the
global variable, the function would just crate a local variable.
z
Scope Examples
z
Scope Examples

More Related Content

PPTX
PDF
Introduction to Higher Order Functions in Scala
PPT
Understanding Synergy Conflicts
ODP
Asynchronous programming with Functional Java and comparison with Scala
Introduction to Higher Order Functions in Scala
Understanding Synergy Conflicts
Asynchronous programming with Functional Java and comparison with Scala

Similar to Variable Scope (20)

PPTX
Function Returns
PPTX
LOCAL VARIABLES AND GLOBAL VARIABLES.pptx
DOCX
Notes of Important Programming Fundamental Questions
PPTX
GlobalLocal_ExceptionHndlng_InPython.pptx
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
PPTX
1.3 - Python ScopePPtforpythonlearners.pptx
PPTX
FUNCTION CPU
PPTX
Javascripts hidden treasures BY - https://geekyants.com/
PPTX
Java script function
PDF
Fork 3.0 JS improvements
PDF
Fork 3.0 JS improvements
PPT
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
PPT
Chapter Introduction to Modular Programming.ppt
PPTX
04. WORKING WITH FUNCTIONS-2 (1).pptx
PDF
Important JavaScript Concepts Every Developer Must Know
Function Returns
LOCAL VARIABLES AND GLOBAL VARIABLES.pptx
Notes of Important Programming Fundamental Questions
GlobalLocal_ExceptionHndlng_InPython.pptx
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
1.3 - Python ScopePPtforpythonlearners.pptx
FUNCTION CPU
Javascripts hidden treasures BY - https://geekyants.com/
Java script function
Fork 3.0 JS improvements
Fork 3.0 JS improvements
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
Chapter Introduction to Modular Programming.ppt
04. WORKING WITH FUNCTIONS-2 (1).pptx
Important JavaScript Concepts Every Developer Must Know
Ad

More from primeteacher32 (20)

PPT
Software Development Life Cycle
PPTX
Returning Data
PPTX
Intro to Functions
PPTX
Introduction to GUIs with guizero
PPTX
Function Parameters
PPTX
Nested Loops
PPT
Conditional Loops
PPTX
Introduction to Repetition Structures
PPTX
Input Validation
PPTX
Windows File Systems
PPTX
Nesting Conditionals
PPTX
Conditionals
PPT
Intro to Python with GPIO
PPTX
Variables and Statements
PPTX
Variables and User Input
PPT
Intro to Python
PPTX
Raspberry Pi
PPT
Hardware vs. Software Presentations
PPTX
Block chain security
PPTX
Software Development Life Cycle
Returning Data
Intro to Functions
Introduction to GUIs with guizero
Function Parameters
Nested Loops
Conditional Loops
Introduction to Repetition Structures
Input Validation
Windows File Systems
Nesting Conditionals
Conditionals
Intro to Python with GPIO
Variables and Statements
Variables and User Input
Intro to Python
Raspberry Pi
Hardware vs. Software Presentations
Block chain security
Ad

Recently uploaded (20)

PPTX
ESD MODULE-5hdbdhbdbdbdbbdbdbbdndbdbdbdbbdbd
PPTX
PE3-WEEK-3sdsadsadasdadadwadwdsdddddd.pptx
PDF
HR Jobs in Jaipur: 2025 Trends, Banking Careers & Smart Hiring Tools
PPTX
1751884730-Visual Basic -Unitj CS B.pptx
PDF
Prostaglandin E2.pdf orthoodontics op kharbanda
PPTX
1-4 Chaptedjkfhkshdkfjhalksjdhfkjshdljkfhrs.pptx
PPTX
Discovering the LMA Course by Tim Han.pptx
PDF
Biography of Mohammad Anamul Haque Nayan
PPTX
E-Commerce____Intermediate_Presentation.pptx
PPTX
The Stock at arrangement the stock and product.pptx
PPTX
退学买新西兰毕业证(WelTec毕业证书)惠灵顿理工学院毕业证国外证书制作
PPTX
Principles of Inheritance and variation class 12.pptx
PPTX
Sports and Dance -lesson 3 powerpoint presentation
PDF
Understanding the Rhetorical Situation Presentation in Blue Orange Muted Il_2...
PPTX
Condensed_Food_Science_Lecture1_Precised.pptx
PPTX
Nervous_System_Drugs_PPT.pptxXXXXXXXXXXXXXXXXX
PDF
Manager Resume for R, CL & Applying Online.pdf
PDF
L-0018048598visual cloud book for PCa-pdf.pdf
PPTX
PMP (Project Management Professional) course prepares individuals
PDF
Josh Gao Strength to Strength Book Summary
ESD MODULE-5hdbdhbdbdbdbbdbdbbdndbdbdbdbbdbd
PE3-WEEK-3sdsadsadasdadadwadwdsdddddd.pptx
HR Jobs in Jaipur: 2025 Trends, Banking Careers & Smart Hiring Tools
1751884730-Visual Basic -Unitj CS B.pptx
Prostaglandin E2.pdf orthoodontics op kharbanda
1-4 Chaptedjkfhkshdkfjhalksjdhfkjshdljkfhrs.pptx
Discovering the LMA Course by Tim Han.pptx
Biography of Mohammad Anamul Haque Nayan
E-Commerce____Intermediate_Presentation.pptx
The Stock at arrangement the stock and product.pptx
退学买新西兰毕业证(WelTec毕业证书)惠灵顿理工学院毕业证国外证书制作
Principles of Inheritance and variation class 12.pptx
Sports and Dance -lesson 3 powerpoint presentation
Understanding the Rhetorical Situation Presentation in Blue Orange Muted Il_2...
Condensed_Food_Science_Lecture1_Precised.pptx
Nervous_System_Drugs_PPT.pptxXXXXXXXXXXXXXXXXX
Manager Resume for R, CL & Applying Online.pdf
L-0018048598visual cloud book for PCa-pdf.pdf
PMP (Project Management Professional) course prepares individuals
Josh Gao Strength to Strength Book Summary

Variable Scope

  • 2. z Variable Scope: Local and Global  Scope refers to the visibility of objects. In other words which parts of your application can see it or use it.  Variables defined inside a function are local to that function. They are hidden from the statements in other functions, which normally cannot access them. When the function terminates the values are released.  Question: What is another situation where the variable was only known while the structure was executing?  Because the variables defined in a function are hidden, other functions may have separate, distinct variables with the same name.  However, do not let this confuse you.  Question: What does the above statement mean?
  • 3. z Local Variables  A function’s local variables exist only while the function is executing. This is known as the lifetime of a local variable.  When the function begins, its local variables and its parameter variables are created in memory, and when the function ends, the local variables and parameter variables are destroyed.  This means that any value stored in a local variable is lost between function calls and posts.
  • 4. z Global Variables and Global Constants  A global variable is any variable defined outside all the functions in a program.  The scope of a global variable is the portion of the program from the variable definition to the end.  This means that a global variable can be accessed by all functions that are defined after the global variable is defined. Bad Programming: The use of global variables Good Programming: Only creating global variables that are constant, passing variables through functions
  • 5. z Updating a Global Variable  Code:  Updating a global variable:  globVar = <val> def functionName (): global globVar globVar = <new value> Why: Without the keyword global inside the function referencing the global variable, the function would just crate a local variable.