NIOS Computer Science: Chapter 12 – Function Part 6

Download PDF of This Page (Size: 281K)

Image shows types ofl variables

Image Shows Types Ofl Variables

Image shows types ofl variables

IV. Global and Local variables

The variable declared in a program may broadly be classified into following two types;

Image of Global and Local variables

Image of Global and Local Variables

Image of Global and Local variables

i) Local variable

A variable declared within the body of a function will be evaluated only within the function.

The portion of the program in which a variable is retained in memory is known as the scope of the variable.

The scope of the local variable is a function where it is defined. A variable may be local to function or compound statement.

ii) Global variable

A variable that is declared outside any function is known as a global variable.

The scope of such a variable extends till the end of the program.

These variables are available to all functions which follow their declaration.

So it should be defined at the beginning, before any function is defined. If in a program, variable a is declared as local as well as global.

Example 4

# include <iostream.h>

int m = 2;

void main ( )

{

int m = 15;

{

int

cout < < “m = ” < < m < < “\n”;

cout < < “ : : m = ” < < : : m < < “\n”

}

cout < < “m = ” < < m < < “\n”;

cout < < “: : m = ” < < : :m < < “\n”;

}The output of the above program is

m= 20

:: m = 2

m = 15

: : m = 2

Variables and Storage class

The storage class of a variable determines which parts of a program can access it and how long it stays in existence.

The storage class can be classified as

Image of Variables and Storage class

Image of Variables and Storage Class

Image of Variables and Storage class