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.

Monday, December 3, 2012

Scope Rules in C++


Scope Rules In C++:

By now we have learned how to declare an UDF (User Defined Function), how to define them, how to call them, what is return type of a function, how to pas & return a value from a function, how to pass & return a reference from a function and how to pass and return pointer from the function, etc.
            While working with the functions so for, in many occasions we have declared variables in different segments of the program. Some time just after the header files mentioned, or sometimes inside the main() function or sometimes in function header or sometimes inside a function body or even sometime declared variables as loop variables. Now the question is – Do declaration of variable(s) in different segments of a program create any difference? Answer to this question you will get after going through the discussion that follows:
            Now in this segment of discussion we will talk about SCOPE RULES. The scope rule of a particular programming language are the rules that decide, in which part(s) of the program a particular piece of code or data item would be known and can be accessed.

Thursday, November 29, 2012

Returning from a function



Returning from a function:     

As invoking or calling a function is important, returning from a function is equally important. As it not only terminates the function’s execution but also passes the control back to the calling function. A function terminates when either a return statement is encountered or the last statement of the function is executed. return statement is used to terminate a function whether it returns a value or not.
            A function can have more than one return statements, but only one of them can get executed, because execution of the function terminates as soon as return statement is encountered.

Returning Values:

All functions except those, whose type is declared as void, must return a value. The data type of the value which is being returned by the return statement of the function should be same as that of return type, mentioned for the function in the function prototype. If the function is about to return an integer value then its return type should be mentioned as int similarly for the float, double, char etc.
Example:

Calling a Function by Passing the Pointer:



Calling a Function by Passing the Pointer:

In this section we will learn about – How to pass pointer to a function definition?
            When a pointer is passed to the function, the address of actual argument in the calling function is copied into the formal argument of the called function (function definition). Therefore here also, the called function does not create its own copy of original values; rather it refers to the original values by the address it receives. To understand this more consider the following example: The following example illustrates the swapping of two values by passing addresses through pointers.  

//Swapping of two values by passing address through pointers

#include <iostream.h>

void swap(int *, int *);                         

//--------------------------------------------

void swap (int *a, int *b)

{

int temp;

temp=*a;

*a=*b;

*b=temp;

}

int main()

{

int x=5;

int y=2;

cout<<"\n Original values were: \n";

cout<<"x = "<<x;

cout<<"\ny = "<<y;

swap(&x,&y);

cout<<"\n\n After swapping: \n\n";

cout<<"x = "<<x;

cout<<"\ny = "<<y;

}

Output:
Original values were:
x = 5
y = 2

After swapping:
x = 2
y = 5