by Dinesh Thakur Category: Functions

Auto

 

The values of the variables are not retained beyond the scope of a function in which they are declared. The values of the variables can be accessed only during the execution of a function. Whatever variables are declared in a function, they are assumed implicitly of auto storage type. For example in the following program segment

 

#include <iostream.h>

void f1()

{

int v3, v4;

.

.

}

void Main()

{

int v1, v2;

f1();

}

 

The variable declaration

                 int v1, v2;

in the Main() function implicitly means

               auto int v1, v2;

and the variable declaration

               int v3, v4;

in the f1() function implicitly means

               auto int v3, v4;

The variables v1, v2 are local to the Main() function and the variables v3, v4 are local to the no function. If the variables are to be retained beyond the scope of a function, static storage type has to be used.

Static

In the case of static type of data, values of the variables declared in a function are retained even after the execution of the function.

Example

The program illustrates auto storage type.

 

#include <iostream.h>

void st();

void main()

{

int v1;

for(v1=0;v1<4;v1++)

{

st();

}

}

void st()

{

auto int sum = 0;

int i;

cout<<"\ninput the value of i ";

cin>>i;

sum=sum+i;

cout<<"The value of sum is ";

cout<<sum;

}

Input and Output:

input the value of i 1

The value of sum is 1

input the value of i 3

The value of sum is 3

input the value of i 5

The value of sum is 5

input the value of i 8

The value of sum is 8

Example

The program illustrates the use of static storage type.

 

II static storage type

#include <iostream.h>

void st();

void main()

{

int v1;

for(v1 =0;v1<4;v1++)

{

st();

}

}

void st()

{

static int sum = 0;

int i;

cout<<"\ninput the value of i ";

cin>>i;

sum=sum+i;

cout<<"The value of sum is ";

cout<<sum;

}

 

Input and Output:

input the value of i 1

The value of sum is 1

input the value of i 3

The value of sum is 4

input the value of i 5

The value of sum is 9

input the value of i 8

The value of sum is 17

Example

Illustrates another program using static storage type.

 

#include <iostream.h>

void sum(int a)

{static int sum = 0;

static int count = 0;

sum = sum +a;

cout<<"count "<<++count;

cout<<'\n';

cout<<"Total sum "<<sum;

cout<<'\n';

}

void main()

{

char policyno[10];

char custname[10];

int amount;

char ch;

do

{

cout<<"Enter policy number";

cin>>policyno;

cout<<"Enter customer name";

cin>>custname;

cout<<"Enter amount";

cin>>amount;

sum(amount);

cout<<"\nDo you want to insert more information. Type y or n. ";

cin>>ch;

}while(ch=='y');

}

 

Input and Output:

Enter policy number P197

Enter customer name Rajesh

Enter amount 1000

count 1

Total sum 1000

Do you want to insert more information. Type y or n. y

Enter policy number P297

Enter customer name Kamal

Enter amount 1200 ,

count 2

Total sum 2200

Do you want to insert more information. Type y or n. n

 

In this program, sum and count are of static storage type. Every time when the information for a new customer is entered, count is incremented by 1 and the amount for which customer has insured is added to the previous value of sum.

 

Extern

 

This type of data does not allocate any memory for a variable but it is just declared. A variable declared as extern type must be used to access a global variable declared in another program file. It must also be used to access a global variable later in the same program.

Example

Illustrates the use of extern storage type.

#include <iostream.h>

void a();

int vl,v2;

void main()

{cout<<"Input the value of vi and v2";

cin>>vl>>v2;

a();

}

void a()

{extern int vi;

extern int v2;

cout<<"Display the value of vi * v2 is ";

cout<<vl *v2;

}





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles