Write a C++ program to demonstrate the static and non static variable usage defining them within a function.
Implementation of the above problem:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr ();
int funct1(int x);
int funct2(int x);
int i,value;
cout<<"WITH STATIC\n";
for(i=1;i<=10;i++)
{
value=funct1(i);
cout<<i<<"\t"<<value<<endl;
}
cout<<"WITHOUT STATIC\n";
for(i=1;i<=10;i++)
{
value=funct2(i);
cout<<i<<"\t"<<value<<endl;
}
getch();
}
int funct1(int x)
{
static int sum=100;
sum+=x;
return sum;
}
int funct2(int x)
{
int sum=100;
sum+=x;
return sum;
}
Thanks
Mukesh Rajput
Post A Comment:
0 comments: