Scope of Variable in C++

Previously we are discussed about to c++ variable. now in this post we are learn about scopes of variable. Normally scope of variable are two type local and global in our source code.

Local variables

local variable are declare normally inside a function and within the block so we are utilize those variable only locally. local variable are active when compiler are execute code on local variable is defined. otherwise this are inactive. When we are perform certain task in our source code help of variables. and they are not need for longer use of other task. then this situation we can use local variable.

About Local variables

1) Local variable are declared in three place. inside a function, inside of block, and form as function parameter. view example.

#include <iostream>
using namespace std;

//local variable no1 ,no2 declared as function parameter
void multiply(int no1,int no2){
  //scope of no1 and no2 are here
  cout<<"Multiply : "<<no1*no2<<endl;
}

int main(){

  /*-------
  + local variable of main function
  + Scope of this variable are only inside main function.
  *-------*/
  int value1=10,value2=20;

    //Passing  two values of multiply function
  multiply(value1,value2);

  {
    //Not allowed access to subject variable outside of this block
    int sub=value1+value2;

    cout<<"Addition : "<<sub;
  }
  return 0;
}
Output
Multiply : 200
Addition : 30

2) local variable are independent in every function.

#include <iostream>
using namespace std;
void foo(){
  //local variable of x and y of foo function
  int x=20,y=40;//integer variables

  cout<<"foo function"<<endl;
  cout<<"x:"<<x<<endl;
  cout<<"y:"<<y<<endl;

}
void bar(){
  //Local Variable of function bar
  int x=90,y=10;//integer variables

  cout<<"bar function"<<endl;
  cout<<"x:"<<x<<endl;
  cout<<"y:"<<y<<endl;

}
int main(){
  //function calling
  foo();
  bar();

}
Output
foo function
x:20
y:40
bar function
x:90
y:10

Note that in two different function we are declare same name of variable. because local variable are not use by the outside the block. compiler are clearly understand the behavior like foo function variable (x,y) is different to bar function variable (x,y).

3) We can used to two same variable within the function. But there the condition of scope and block will different. see the example.

#include <iostream>
using namespace std;

int main(){

  //local variable x
  int x=15;
  cout<<"Main x  :"<<x<<endl;
  
  {
    //local block variable x
    int x=23;
    cout<<"Block x :"<<x;
  }
  return 0;
}
Output
Main x  :15
Block x :23

Note that both x are local variable. Inside variable are accessible only this block. Observe that when we declare secondary variable x then previously defined variable x not work. So in this situation try to avoid to use same name of variable.

4) If we are not assign any value to local variables. then compile are allocate default value of all uninitialized normal value is garbage. but variable are static variable then it change the default value. see example of normal variable.

#include <iostream>
using namespace std;
int main(){
  //local variable x
  int x;
  //declared the variable x but not assign any value
  cout<<x; //garbage value
  return 0;
}
Output : print garbage value.
4200875

Global variables

Global variable is defined on outside of function. they are directly accessible by any function and blocks. most of case global variable are defined the after the header file in source code.

About Global variables

Scope of global variable are until program execution. default value of global variable are not garbage. see an example.

#include<iostream>
using namespace std;
//declared global variable
bool status;
char symbol;
int age;
float salary;
double population;
int *ptr;
int main(){
  //Display value of variables
  cout<<" bool status :"<<status<<endl;
  cout<<" char symbol :"<<symbol<<endl;
  cout<<" int age   :"<<age<<endl;
  cout<<" float salary :"<<salary<<endl;
  cout<<" double population :"<<population<<endl;
  cout<<" int *ptr :"<<ptr<<endl;
  return 0; 
}
Output
 bool status :0
 char symbol :
 int age   :0
 float salary :0
 double population :0
 int *ptr :0

Common mistake

New programmer, which are starting to learn programming. they are use lot of global variables in our source code. Because global variable are no need to pass any function and we can directly utilize those value. But there is not a good way. because all global variable are utilize anywhere in our source code. Assume that if we are change global variable value at any place so there are possible to difficulty on trace the code and logical error.

© 2019, regularcodes.com, All rights reserved