void init1()
{
 static int yrs = 1;
 cout << "The value of yrs is " << yrs << endl;
 yrs = yrs + 2;
 return;
}
void init2()
{
 static int yrs;
 yrs = 1;
 cout << "The value of yrs is " << yrs << endl;
 yrs = yrs + 2;
 return;
}

Recommended Answers

All 4 Replies

Sounds like homework.

What do you think the implications of splitting up line 3 is? What does static do?

I think the first one does 1,3,5 and so on
when the second would only do 1,1,1,1

Good! In the first example, the value isn't set. It's initialized. In the second example, the value is set on each call.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.