Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

C++ Scope of Variables

In this tutorial, we will learn about Scope of variables, Local variables, Global variables, difference in local & global variables, Advantages/disadvantages of local & global variables in C++ with the help of examples.


Scope of Variable

Scope is a region/area of the program where the variable is valid.
In other words, Variable Scope is a region in a program where a variable is declared and used.


Types of Variables Scopes

There are mainly two types of variable scopes:
1) Local Variables
2) Global Variables


Local Variables

Variables that are declared inside a function or a block are called Local Variables.

1) Local variables must be declared before they have used in the program.
2) Local variables can only be used inside the function or block.
3) These variables are created when the “function is called” or “the block in entered” and destroyed after exiting from “the block” or when the “call returns from the function”.
4) Initilisation of Local variables is mandatory.

For Example:
// declaration of local variable
int main() {
     int x = 2; // Local variable
}

Global Variables

1) Global variables are declared outside the function.
2) Any function i.e. both local function and global function can change the value of global-variables.
3) They are available through out the life time of a program.
4) Global variables can be accessed from any part of the program.

For Example:
// declaration of Global variables
int y = 10; // Global variable
int main() {
    int x = 5; // Local variable
}

Local Variables vs Global Variables

Parameter Local Variables Global Variables
Scope It is declared inside a function. It is declared outside the function.
Value If it is not initialized, a garbage value is stored. If it is not initialized, zero is stored as default.
Memory Storage It is stored on the stack unless specified. It is stored on a fixed location decided by the compiler.
Accessed It can be accessed only inside the function. It can be accessed form any part of the program.
Lifetime It is created when the function starts execution and end when the function terminate. It is created before the program’s global execution starts and end when the program terminates.
Data sharing Data sharing 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.
Parameters Parameters passing is required for local variable to access the value in other function. Parameters passing is not necessary for a global variable as it is visible throughout the program.

Advantage of local variables

1) The use of local variables offer a guarantee that the values of variables will return intact while the task is running.
2) local variables are deleted as soon as any function is over and release the memory space it occupies.
3) we can give local variables the same name in the different functions because they are only recognized by the function they are declared in.


Disadvantages of local variables

1) The debugging process of a local variable is quite tricky.
2) Common data required to pass repeatedly as data sharing is not possible between modules.
3) They have a very limited scope.


Advantage of Global variables

1) It is ideally used for storing “constants” as it helps you keep the consistency.
2) A Global variable is useful when multiple functions are accessing the same data.
3) You only require to declare global variable single time outside the modules.


Disadvantages of Global variables

1) Data can be modified by any function.
2) Any Statement written in the program can change the value of the global variable.
3) Too many variables deeclared as global, then they remain in the memory till program execution is completed. this may cause of Out of Memory issue.

we will discuss local variables and global variables in later tutorials.

Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!