Thread: Breaking out of functions

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    43

    Breaking out of functions

    I have a program which I am trying to use to detect if there is a temporary disconnect from the internet. If there is then once the connection is detected a function called recovery() is called to set up and initalise things that will have broken. Throughout the recovery function I need to constantly be checking that the connection is still working and has not broken again half way through the recovery process. So what I proposed was something like the code below.

    The idea is to start the recovery function from the beginning if at any time a disconnect occurs a second time. However as I understand it, if recovery calls recovery from itself then once the second recovery has finished, then the first recovery will try to continue from where it left off. Therefore I need something in recovery that breaks out of itself and goes back to whatever function called it originally. I considered using break however I believe this only breaks out of the IF statement and not the whole function. Can anyone tell me of a function a bit like Break but that exits the whole function (but not the whole program).

    Thankyou in advance for your help,
    Chris

    Code:
    void recovery(void){
    
    //Some other code goes here
    
    	if(connected == NULL)
    	{
    		recovery();
    		break;
    	}
    
    
    
    //Insert other code goes in here
    
    
    	if(connected == NULL)
    	{
    		recovery();
    		break;
    	}
    
    //more code here
    
    
    //if statement again here etc....
    
    
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What you want is return. Just like break, but it exits the current function, returning any optional value you want to. Examples:
    Code:
    void foo( void )
    {
        ...
        return; /* function returns 'void', that is to say, nothing... */
    }
    
    ...
    
    int bar( void )
    {
        int x;
        ...
        return x; /* function returns an int, here we decide to return whatever 'x' has in it... */
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    As quazah said, you need to return a value to break out of a function. If you want the function to restart, just make it return -1 (for error). In that case you can call it with the line:
    while(recovery());
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  2. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  3. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  4. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  5. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM