Loading....
Coupon Accepted Successfully!

 

Scope rules

Local & Global variables

Local

A local variable is a variable that is given local scope. Such variables are accessible only from the function or block in which it is declared. Local variables are contrasted with global variables. Local variables are special because in most languages they are automatic variables stored on the call stack directly. This means that when a recursive function calls itself, local variables in each instance of the function are given separate memory address space. Hence variables of this scope can be declared, written to, and read, without any risk of side-effects to processes outside of the block in which they are declared.

Example

void do_it()
{
int x;
x = 100;
}
Main ()
{
printf(“Local variable x of do_it() method is = %d”,do_it());
}

 

Global

These variables can be accessed (i.e. known) by any function comprising the program. They are implemented by associating memory locations with variable names. They do not get recreated if the function is recalled. Global variables are used extensively to pass information between sections of code that don't share a caller/callee relation like concurrent threads and signal handlers. In Other word Global variables are those, which are available throughout the program's execution. They are not defined inside any block. They exist in memory until the program is in memory.

 

Example:
#include
int global = 3; // This is the global variable.
void ChangeGlobal()
{
global = 5; // Reference to global variable in a function.
}
int main()
{
printf ("°/od ", global); // Reference to global variable in another function.
ChangeGlobal ();
printf (" %d", global);
return 0;
}

 

*-------------------Output---------------------*

3 5

Example:
#include
int k;
void do_less()
{
printf("\nk= %d\n",k);
k=19;
}
main()
{
k=91;
do_ less();
printf("\n now k= %d\n",k);
}

 

*-----------------Output------------------------*

k= 91

now k= 19

In the above example k is not defined in any block but in the beginning. So it is available throughout the program.

Parameter passing technique

Parameters are the values upon which we want the function should operate These are passed to functions within parenthesis. The mechanism by which parameters are passed to function is known as parameter passing. There are basically 2-types of techniques

  • Call by value
  • Call by referance

Call by value

In call by value method if the formal parameters are changed then the values in calling function will not change. This is because formal parameters are local to the function, and actual parameters are local to the calling function. But there are certain cases we need to change the value of the variable from inside a function. This is usually done in pass by reference methode or else it can be done only incase of global variable. Only global variables can be changed from within function. Here an interesting thing comes to mind. If we have defined a global variable and a local variable with same name then if we change the value then whose value will be changed, let us see it. Consider the following examples.

Example:
main ()
{
int a = 10, b=20;
swapy (a,b);
printf ("\na = % d b = % d", a,b);
}
swapy (int x, int y)
{
int t;
t = x;
x = y;
y = t;
printf ("\n x = % d y = % d", x, y);
}

 

*------------------Output----------------------*

x = 20 y = 10

a =10 b=20

Call by Reference

Call by reference is implemented indirectly by passing the address of the variable. implement the function indirectly. This is done by passing the address of the variable and changing the value of the variable through its address.

 

Example:
main ()
{

int a = 10, b =20,
swapr (&a, &b);
printf ("\n a = %d b= %d", a, b);
}
swapr (int *x, int * y)
{
int t;
t= *x
*x = *y;
*y = t;
printf ("\n x = % d y = % d", *x, *y);

}

 

*------------------Output-----------------------*

x =20 y =10

a = 20 b=10





Test Your Skills Now!
Take a Quiz now
Reviewer Name