#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2

    How to return to main()?


    let's say the main() calls for function1, and function1 calls for function 2.
    is there a way to return (not necessarily with a value) directly to main?

    for example:

    Code:
    int main()
    {
       code...
       code...
       func1();
       code..
    }
    
    func1()
    {
       code...
       func2();
    }
    
    func2()
    {
       code...
       return; //returning to main()
    }
    is there a command that takes the program back to main?

    thanks in advanced!
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    Perhaps it would be better for main() to call func2() directly.

    There are the setjmp() and longjmp() functions, but you should really work on your program structure before resorting to using these functions.

    Your example shows func1() returning anyway after calling func2(), so really there is nothing else to do.

    Even if func1() did do something (say based on the success/fail of func2()), this is easily achieved (and the best approach) by having func2() return a result, and for func1() to test that result.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Or, trivially, you could have func1 return immediately after calling func2.

    Each function call pushes that function's block onto the stack and as each function returns it pops its block off of the stack. Attempting to bypass normal calls and returns means that you also need to clean up the stack on your own. Why wish for such a mess?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2
    thanks guys.

    Even if func1() did do something (say based on the success/fail of func2()), this is easily achieved (and the best approach) by having func2() return a result, and for func1() to test that result.

    OK, yeah, that makes much more sense.
    thanks for the tip. :cool:

IMN logo majestic logo threadwatch logo seochat tools logo