Static variables in functions

This is a discussion on Static variables in functions within the C++ Programming forums, part of the General Programming Boards category; I'm trying to use static variables in a recursive function, but am having a problem. I've got something very loosely ...

  1. #1
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728

    Static variables in functions

    I'm trying to use static variables in a recursive function, but am having a problem. I've got something very loosely like this:
    Code:
    int recursive(int numOfTimes) 
    {
        static int counter;
        if (!numOfTimes)
          counter=0;
    
        ..... // Do some calculations here that increment counter
    
        if (numOfTimes<10)
           someVar=recursive(numOfTimes+1);
    
        ..... // Do some calculations using counter that depend on
        ..... // how high counter got in the previous recursive calls
    
        return blech;
    }
    My program is very different than this example shows the problem: how do I make it so counter isn't reset every time the recursive function is called? I haven't tried this program yet (one of the problems with work and school, only time for programming on the weekends. But it seems like every time the next recursive function is called, this would reset counter to garbage once it hits the line "static int counter". If so, any suggestions to get around this?

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    You have to initialize it! Like this
    Code:
    #include <iostream>
    
    void test();
    
    int main()
    {
      for(int i = 0; i < 10; i++)
        test();
    
      return 0;
    }
    
    
    void test()
    {
    //Initialize here, this next line will only execute the first time you invoke test()
    static int number = 0;
    number++;
    std::cout << "number is now " << number << endl;
    }

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Really?? Wow, I didn't realize that line would be skipped over each function call after that, I thought it would constantly set it to zero each time it passed by! Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about static members and functions
    By e66n06 in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2008, 02:41 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Linking errors with static var
    By Elysia in forum C++ Programming
    Replies: 8
    Last Post: 10-27-2007, 06:24 PM
  4. Order of initialization of static variables
    By Sang-drax in forum C++ Programming
    Replies: 4
    Last Post: 06-20-2004, 05:31 PM
  5. Static functions
    By Morgan in forum C Programming
    Replies: 2
    Last Post: 01-20-2003, 05:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21