views:

927

answers:

8

I have a recursive function with a static variable "count". The function increments count recursively and since it has file scope, when I call foo() a second time, count is still equal to 5. Is there a technique to reset count to 0 before the second time foo() is called?

Basically, I don't want count to have file scope but I want it to retain its value through different iterations.

One way I can think of doing it is have an argument in foo() to initialize foo(). Such as foo(int count). But is there another way?

#include <iostream>

using namespace std;

void foo()
{
    static int count = 0;

    if(count<5)
    {
        count++;
        cout<<count<<endl;
        foo();
    }
    else
    {
        cout<<"count > 5"<<endl;
    }
}

int main()
{
    foo();  //increment count from 0 to 5
    foo();  //count is already at 5

    return 0;
}
+5  A: 

Put it in the else

else
{
    cout<<"count > 5"<<endl;
    count = 0;
}

If you want to recursion properly check out waxwing's answer.

Martin York
This sounds like the best way. (+1)
TheMachineCharmer
+4  A: 

Instead of using a static variable, simply pass count as an argument.

void foo(int count) {
    if (count < 5) {
     count++;
     cout << count << endl;
     foo(count);
    } else {
     cout << "count > 5" << endl;
    }
}

int main() {
    foo(0);
    foo(0);
}

Static variables and recursion do not generally go together.

Daniel Bingham
A: 

You can change foo to accept a boolean variable which means reset or not.

void foo() {
  foo(false);
}

void foo(int b)
{
    static int count = 0;

    if(b) { count = 0 };

    ...
}

call foo() as before, or foo(true), if you want to reset it.

Zed
+13  A: 

A more idiomatic way is to split it into two functions:

void foo() {
   foo_recursive(0);
}

void foo_recursive(int count) {
    if (count < 5) {
        count++;
        cout << count << endl;
        foo_recursive(count);
    } else {
        cout << "count > 5" << endl;
    }
}

Which has the benefit of not requiring the caller to supply an argument to foo() and also you don't need a static variable (which I always feel is a bad idea).

waxwing
This is probably the best way to do it. +1 Unless there's some strange reason that he needs a static variable for this...
Daniel Bingham
Argument defaults are there so you don't have to make a new function. We're using C++ here. void foo( int count = 0 ) {
Potatoswatter
Default arguments should default choice values that essentially are on the side of the caller. The argument in this case, however, is an implementation detail, and should be hidden behind another function. You don't want the caller pass "100" as count, do you?
Johannes Schaub - litb
A: 

You can make the function auto-resetting this way:

void foo () {
  if (count >= 5) {
    cout<<"count >= 5"<<endl;
    count = 0;
    return;
  }

  cout<<++count<<endl;

  foo();
}

Saves boilerplate code.

André Neves
A: 
void foo() {
  ...
  if (count > 0) count--; // you can decrease it at then end of foo()
}
Nick D
+1  A: 

How about this

count = (count+1)%5
TheMachineCharmer
A: 

No need to declare two function or use static variable. You could use default arguments.

// Use Default arguments
void foo( int count = 0);

void foo( int count )
{
    if(count<5)
    {
        count++;
        cout<<count<<endl;
        foo(count);
    }
    else
    {
        cout<<"count > 5"<<endl;
    }
}

int main()
{
    foo(); // by default argument =0
    foo(); // by default argument =0

    return 0;
}
Kirill V. Lyadvinsky