If you like my posts please leave a comment.

Saturday, February 9, 2013

Determining Scope of a Variable


Determining Scope of a Variable:

How to determine a scope of a variable or function?


The scope for a function or variable can be determined by its place of declaration. Let’s consider –
A function’s (say X()’s) declaration appears within another function (say Y()), then the function X() is local to function Y() i.e it is locally available to function Y() or you can say that it can only be called from function Y(). Such function’s prototypes are local prototypes and their scope is local to the function that contains their declaration. On the other hand, if the function (say X()’s) prototype appears outside all other functions in the program file, then the function X() can be accessed from any of the functions in the file. Such function prototypes are global prototype and are globally available to all the functions in the file.
          As it is said earlier, the scope for variable is also determined by the place of their declaration. If a variable is declared outside all functions, it is said to be global variable. A global variable is available to all functions and blocks defined in the. A global variable comes into existence when program runs and is destroyed when the program terminates. Global variables hold their values throughout the program execution. Any expression may access them regardless of what block of code that expression is in. They can be accessed from anywhere in the file. Using of global variable at the earlier stage of learning programming is my be ok, but using of global variable in a program is not encouraged much as data items become more vulnerable to accidental or unwanted manipulation.
          Unlike the global variables, the local variables are the ones that are defined within a function body. A local variable comes into existence when the function in which it is declared executes and destroyed upon exit. A value stored in local variable is lost when the function in which it declared exits. It is declared and defined every time function is called or invoked.
          However static local variable can retain its value even after function exits. Such variables are defined and initialized at the time of function call and can retain its value throughout the program run, but remember its scope is still local (function scope).
          Here in this context one thing more I would like to point out that, a local variable’s name can be same as that of a global variable’s name. But in this case function will able to access its local variable and the global variable which is having the name as that of a local variable remains hidden.
To unhide or to access the global variable :: (scope resolution operator) is used. Consider the following example to understand this concept more.

#include <iostream.h>

int x = 50; //global variable ‘x’ having file scope
int main()
{
int x = 100; //Local variable ‘x’ having function scope
cout<<::x<<'\n'<<x<<endl; //global variable ‘x’ is made unhidden using
                             //::x
return 0;
}
 Now the above code should produce the following result:
50
100

Now lets consider the another example to understand the behaviour of a static local variable: 

  #include <iostream.h>
  #include <stdlib.h>
  #include <conio.h>
  void count(void)
  {
  static int C=0;
  int a=0;
  C++;
  a++;
  cout<<"the value of C is = "<<C<<endl;
  cout<<"the value of a is = "<<a<<endl;
  }
  void main()
  {      system(“cls”);
     for(int i=0;i<5;i++)
     count();
     getch();
  }
Output:
Observe the above output carefully. The function count() is executed 5 times. Each time variable ‘C’ retains its previous value and increments it by one (1) but the normal variable ‘a’ each time initialized to 0 (zero) and thus every time incremented by one (1) only.

No comments:

Post a Comment