Thread: Retroactive function

  1. #1
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694

    Retroactive function

    I have a function in a BST.
    Code:
    void fuc(...)
    {
        int flag;
        flag=0;
        if(terminating_condition)
        {.....}
        if(...)
        {
           flag=1;
           fuc(...);
         }
         if(flag==1) do_something;
    }
    The problem is that because fuc calls herself flag is going to be always 0.I want to do_something everytime i call her,except first time i call her.No global and etc variables are allowed for me.I cannot add more arguments to fuc.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You want static variable?

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Forgive my ignorance but ... wtf is a BST?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    BST = binary search tree.

    If you don't like the static variable approach, there's always writing a second function to do the real work.
    Code:
    void func() {
        func_that_does_the_real_work(0);
    }
    
    void func_that_does_the_real_work(int flag) {
        ... real work
    }

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    BST = binary search tree.
    Ahhh... thanks. Yet another silly acronym for my collection

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by tabstop View Post
    BST = binary search tree.

    If you don't like the static variable approach, there's always writing a second function to do the real work.
    Code:
    void func() {
        func_that_does_the_real_work(0);
    }
    
    void func_that_does_the_real_work(int flag) {
        ... real work
    }
    I did not understand you. :/

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Bayint Naung View Post
    You want static variable?
    Probaply i am forced to.However how is this going to work.I kave to initialize this static variable in my function.I want it to be used like a counter.But i write i=0; then everytime my function call herself the initialization is going to take place again.
    Last edited by std10093; 05-22-2011 at 04:34 AM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void foo( void )
    {
        static int bar = 1;
        printf( "bar is: %d\n", bar );
        if( bar )
            bar = 0;
    }
    You could always throw a quick piece of code together and see how it works.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by quzah View Post
    Code:
    void foo( void )
    {
        static int bar = 1;
        printf( "bar is: %d\n", bar );
        if( bar )
            bar = 0;
    }
    You could always throw a quick piece of code together and see how it works.


    Quzah.
    Understood

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Going forward you should note that this is a recursive function, not a retroactive function. Knowing this may help you in future searches.

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by std10093 View Post
    The problem is that because fuc calls herself flag is going to be always 0.I want to do_something everytime i call her,except first time i call her.No global and etc variables are allowed for me.I cannot add more arguments to fuc.
    Then the problem is incorrectly specified. If you can't use a global and you can't add a parameter, the only thing left is to make a copy of this function which is used for the first level of recursion, then it calls the other function for the other levels. Which of course is stupid, but they teach you to be stupid in school, so no surprise there.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 06-09-2009, 02:19 AM
  2. Replies: 2
    Last Post: 02-26-2009, 11:48 PM
  3. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 05:03 PM
  4. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  5. Replies: 9
    Last Post: 01-02-2007, 04:22 PM