title: Recursive function with static variable
body: <p>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?</p>
  
  <p>Basically, I don't want count to have file scope but I want it to retain its value through different iterations.</p>
  
  <p>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?</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  void foo()
  {
      static int count = 0;
  
      if(count&lt;5)
      {
          count++;
          cout&lt;&lt;count&lt;&lt;endl;
          foo();
      }
      else
      {
          cout&lt;&lt;"count &gt; 5"&lt;&lt;endl;
      }
  }
  
  int main()
  {
      foo();  //increment count from 0 to 5
      foo();  //count is already at 5
  
      return 0;
  }
  </code></pre>