LOCAL VARIABLES
&
GLOBAL VARIABLES
Presented by:- Atul Sahu
What is a Variable ?
A variable is a data name that may be used to store a data value. A
variable may take different values at different times of execution and may be
chosen by the user. it may consist of letters, digits and underscore character.
•Importance in programming
Variables are essential for managing data in a program. they enables
developers to store values of various data types, such as strings, integers and
floats, which can be used to perform calculations manipulate text, or control
program flow.
Scope of Variable
Each variable is defined and can be used within its scope and
determines that wherein the program this variable is available to
use. The scope means the lifetime of that variable. It means the
variable can only be accessed or visible within its scope.
The scope of variables can be defined with their declaration, and
variables are declared mainly in two ways.
Local Variable
Local variables are variables that are declared within a specific scope,
such as inside a function or a block of code.
Example-
#include<stdio.h>
Void main()
{
Int X=24, Y=4;
Printf(“X=%d and Y=%d”,X,Y);
}
Global Variable
Global variables are variables that are declared outside of any function
or block of code.
Example-
#include<conio.h>
Int A=10, B;
Void main()
{
Printf(‘A=%d and B=%d”,A,B);
}
LOCAL VARIABLES AND GLOBAL VARIABLES.pptx
It is declared inside a function. It is declared outside the function.
If it is not initialized, a garbage value
is stored.
If it is not initialized zero is stored as default.
It is created when the function starts
execution and lost when the functions
terminate.
It is created before the programs global execution starts
and lost when the program terminate.
Data sharing is not possible as data of
the local variable can be accessed by
only one function.
Data sharing is possible as multiple functions can access
the same global variable.
When the value of the local variable is
modified in one function, the changes
are not visible in another fuction.
When the value of the global variable is modified in one
function, the changes are visible in rest of the program.
Local variables can be accessed inside
a function in which they are declared.
you can access global variables be any statement in the
program.
WHEN TO USE LOCAL OR GLOBAL VARIABLE -
If you feel that a certain variable will take multiple
values by passing through various functions then use local
variables . If you feel that a certain variable you need to
use will have constant value, then declare it as a global
variable.
Advantages Of Local Variable:-
The same name of a local variable can be used in different functions as it is
only recognized by the function in which it is declared. Local variables use
memory only for the limited time when the function is executed, after that
same memory location can be reused
Disadvantages Of Local Variable:-
The scope of the local variable is limited to its function only
and cannot be used by other functions. Data sharing by the
local variable is not allowed.
Advantages Of Global Variable:-
Global variables can be accessed by all the functions present in the
program. Only a single declaration is required. Very useful if all the
functions are accessing the same data.
Disadvantages Of Global Variable:-
The value of a global variable can be changed accidently as it can
be used by any function in the program. If we use a large number of
global variables, then there is a high chance of error generation in
the program.
Example Of Local & Global Variable In Python:
Thank you

More Related Content

DOCX
Programming Global variable
PDF
SPL 9 | Scope of Variables in C
PPT
SD & D Variable Scope

Similar to LOCAL VARIABLES AND GLOBAL VARIABLES.pptx (20)

PPT
Lecture 14 - Scope Rules
PPTX
subash.ppt.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
Functions in Python.pdfnsjiwshkwijjahuwjwjw
DOCX
PDF
Data structure scope of variables
PPTX
Function BPK2
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
Global variables, sorting static variables,function and arrays,
PPTX
Functions-(Global-Local-Nonlocal) (1).pptx
PPTX
Function Returns
PPTX
GlobalLocal_ExceptionHndlng_InPython.pptx
PPTX
Scope rules : local and global variables
Lecture 14 - Scope Rules
subash.ppt.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...
Functions in Python.pdfnsjiwshkwijjahuwjwjw
Data structure scope of variables
Function BPK2
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...
Global variables, sorting static variables,function and arrays,
Functions-(Global-Local-Nonlocal) (1).pptx
Function Returns
GlobalLocal_ExceptionHndlng_InPython.pptx
Scope rules : local and global variables
Ad

Recently uploaded (20)

PPTX
Software Engineering and software moduleing
PDF
Computer organization and architecuture Digital Notes....pdf
PPTX
CN_Unite_1 AI&DS ENGGERING SPPU PUNE UNIVERSITY
PPTX
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
PDF
Applications of Equal_Area_Criterion.pdf
PDF
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf
PPTX
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
PPTX
Module 8- Technological and Communication Skills.pptx
DOC
T Pandian CV Madurai pandi kokkaf illaya
PPTX
Measurement Uncertainty and Measurement System analysis
PDF
Unit I -OPERATING SYSTEMS_SRM_KATTANKULATHUR.pptx.pdf
PPTX
Principal presentation for NAAC (1).pptx
PDF
Unit1 - AIML Chapter 1 concept and ethics
PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
PPTX
A Brief Introduction to IoT- Smart Objects: The "Things" in IoT
PDF
Introduction to Power System StabilityPS
PDF
August -2025_Top10 Read_Articles_ijait.pdf
PPTX
PRASUNET_20240614003_231416_0000[1].pptx
PDF
August 2025 - Top 10 Read Articles in Network Security & Its Applications
PPTX
Amdahl’s law is explained in the above power point presentations
Software Engineering and software moduleing
Computer organization and architecuture Digital Notes....pdf
CN_Unite_1 AI&DS ENGGERING SPPU PUNE UNIVERSITY
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
Applications of Equal_Area_Criterion.pdf
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
Module 8- Technological and Communication Skills.pptx
T Pandian CV Madurai pandi kokkaf illaya
Measurement Uncertainty and Measurement System analysis
Unit I -OPERATING SYSTEMS_SRM_KATTANKULATHUR.pptx.pdf
Principal presentation for NAAC (1).pptx
Unit1 - AIML Chapter 1 concept and ethics
"Array and Linked List in Data Structures with Types, Operations, Implementat...
A Brief Introduction to IoT- Smart Objects: The "Things" in IoT
Introduction to Power System StabilityPS
August -2025_Top10 Read_Articles_ijait.pdf
PRASUNET_20240614003_231416_0000[1].pptx
August 2025 - Top 10 Read Articles in Network Security & Its Applications
Amdahl’s law is explained in the above power point presentations
Ad

LOCAL VARIABLES AND GLOBAL VARIABLES.pptx

  • 2. What is a Variable ? A variable is a data name that may be used to store a data value. A variable may take different values at different times of execution and may be chosen by the user. it may consist of letters, digits and underscore character. •Importance in programming Variables are essential for managing data in a program. they enables developers to store values of various data types, such as strings, integers and floats, which can be used to perform calculations manipulate text, or control program flow.
  • 3. Scope of Variable Each variable is defined and can be used within its scope and determines that wherein the program this variable is available to use. The scope means the lifetime of that variable. It means the variable can only be accessed or visible within its scope. The scope of variables can be defined with their declaration, and variables are declared mainly in two ways.
  • 4. Local Variable Local variables are variables that are declared within a specific scope, such as inside a function or a block of code. Example- #include<stdio.h> Void main() { Int X=24, Y=4; Printf(“X=%d and Y=%d”,X,Y); }
  • 5. Global Variable Global variables are variables that are declared outside of any function or block of code. Example- #include<conio.h> Int A=10, B; Void main() { Printf(‘A=%d and B=%d”,A,B); }
  • 7. It is declared inside a function. It is declared outside the function. If it is not initialized, a garbage value is stored. If it is not initialized zero is stored as default. It is created when the function starts execution and lost when the functions terminate. It is created before the programs global execution starts and lost when the program terminate. Data sharing is not possible as data of the local variable can be accessed by only one function. Data sharing is possible as multiple functions can access the same global variable. When the value of the local variable is modified in one function, the changes are not visible in another fuction. When the value of the global variable is modified in one function, the changes are visible in rest of the program. Local variables can be accessed inside a function in which they are declared. you can access global variables be any statement in the program.
  • 8. WHEN TO USE LOCAL OR GLOBAL VARIABLE - If you feel that a certain variable will take multiple values by passing through various functions then use local variables . If you feel that a certain variable you need to use will have constant value, then declare it as a global variable.
  • 9. Advantages Of Local Variable:- The same name of a local variable can be used in different functions as it is only recognized by the function in which it is declared. Local variables use memory only for the limited time when the function is executed, after that same memory location can be reused Disadvantages Of Local Variable:- The scope of the local variable is limited to its function only and cannot be used by other functions. Data sharing by the local variable is not allowed.
  • 10. Advantages Of Global Variable:- Global variables can be accessed by all the functions present in the program. Only a single declaration is required. Very useful if all the functions are accessing the same data. Disadvantages Of Global Variable:- The value of a global variable can be changed accidently as it can be used by any function in the program. If we use a large number of global variables, then there is a high chance of error generation in the program.
  • 11. Example Of Local & Global Variable In Python: