C++ Scope of Variables

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.

Next Tutorial

We hope that this tutorial helped you develop better understanding of the concept of Scope of Variable in C++.

Keep Learning : )

In the next tutorial, you'll learn about C++ Keywords and Identifiers.

- Related Topics