Thread: Any way to return to the top of main from a function

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    19

    Any way to return to the top of main from a function

    Hello everyone. Sorry if this is a stupid question.

    Let's say that I have a program which consists of many different branches of functions. Is there any simple way to make a statement or set a condition to return to the top of main (essentially restarting the program) from a separate function without using a loop in main?

    I read up on the goto statement but apparently this only works within the same function. I am looking for something similar that can hop across functions I suppose.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    yea put the code you want in main in another function, and then just call that function in main once, then call recursively as many times as needed.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    I see what you're driving at, but then won't I have to hop back through a couple functions anyway to return to the top of the new "main" function.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You don't want to be calling main() again. No!

    Main calls a menu or driver function, and that has your loops, switch statements, and calls all the top level functions.

    Those top level functions may have their own sub menu's and or directly call some second level functions, themselves.

    But you never call main, again. Loop back, or forget it.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    On another note, avoid using goto if you can. Because goto can jump all over the place, it's often considered bad practice to use it. It can cause messy code.
    Last edited by Elysia; 04-28-2008 at 03:37 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is this what you want to do?
    (Note, pseudo-C to clarify the problem, it won't compile)
    Code:
    int main()
    {
    label:
        func1();
    }
    
    func1()
    {
       dostuff();
    }
    
    dostuff()
    {
        choice = menu();
        if (choice == '9') goto label;
    }

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Why are we using goto matsp? It works, but since they are meant to be avoided wouldent a loop would be better:
    Code:
    int again()
    {
         //if says yes then return 0 
    }
    
    int main()
    {
         // Do initailisation here    
         int done = 0;
         while(done == 0)
         {
              // Do stuff to loop over here
              done = again();
         }
         return 0;
    }

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think mats was trying to ask if the sort of behaviour posted was what the OP wanted to achieve. I don't think mats was trying to tell the OP in any way to do it that way.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, I was trying to understand what the original poster actually wants to achieve - not in any way suggesting the solution in the code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Ok sure, I just re-read the op's post. tbh, I see no need to jump to the top of main from inside one of these functions. IMO A better structure would be that when the user wants to reset the program to break out of the inner function to main, which then loops back to the start.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mike_g View Post
    Ok sure, I just re-read the op's post. tbh, I see no need to jump to the top of main from inside one of these functions. IMO A better structure would be that when the user wants to reset the program to break out of the inner function to main, which then loops back to the start.
    Sure, but before we can issue a right solution, we need to understand what the ACTUAL problem is - there are several different possibilities - but the right one for one scenario isn't the right one for another one.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I think the op wants to break out of functions several layers deep, which would require code to break out in all the calling functions. Something like:

    Code:
    int IsEnd()
    {
        // if is end return 1
    }
    
    int MoreStuff()
    {
        if( IsEnd() ) return 1;
        // Do stuff
        return 0;
    }
    
    int DoStuff()
    {
        if( MoreStuff() ) return 1;
        // Do stuff
        return 0;
    }
    
    int main()
    {
        while(! done)
            DoStuff(); 
    }

  13. #13
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I would have thought they want to call main recursively.

    Code:
    void go(void)
    {
        /* ... */
    
        go();
    
        return;
    }
    
    
    int main(void)
    {
        go();
    
        return 0;
    }
    Well it's a guessing game at the moment.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by zacs7 View Post
    I would have thought they want to call main recursively.

    Code:
    void go(void)
    {
        /* ... */
    
        go();
    
        return;
    }
    
    
    int main(void)
    {
        go();
    
        return 0;
    }
    Well it's a guessing game at the moment.
    That is the solution suggested in post #2, but that's not "essentially restarting the program", which is what post #1 mentions. It may of course just be that I'm reading too much into a not so clearly written quesiton...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If I've understood the problem correctly, there are basically two ways to "restart" the program. You can use recursion as zacs7 and Raigne suggested; but this means that you might end up with a stack overflow if you do it too often. It also uses lots of unnecessary memory and so on. In general, this is the last way I'd do this.

    What I would do is what mike_g suggested: use return values to communicate up the stack that you want to restart the program. It's not too difficult, actually, unless you're already using return values extensively; in that case, you might want to set a flag instead.

    Another alternative that hasn't been mentioned is to use longjmp(), a sort of cross-function goto -- but there's a reason it hasn't been mentioned, and that is because this is not a good function to use at all!

    Try using return values and see how it goes.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM