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
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: