Thread: Return 0 in a void function?

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    19

    Return 0 in a void function?

    I have a set of commands in a function that i want to end in return 0;
    and i am using a void function. is there a work around to have a "return 0" type command in the function because i want it to exit the program

  2. #2
    Registered User
    Join Date
    Jun 2012
    Location
    Here
    Posts
    23
    In void function just write 'return'

    Code:
    void someFunction(void)
    {
    /* some code here */
    
    return;
    }

  3. #3
    Registered User
    Join Date
    Jun 2012
    Location
    Here
    Posts
    23
    Sorry I must've misunderstood you. Do you mean exit the entire program? Or just exit the function?

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    19
    Quote Originally Posted by heinz55 View Post
    In void function just write 'return'

    Code:
    void someFunction(void)
    {
    /* some code here */
    
    return;
    }
    I would like to exit the program. I tried return; and it only exits the function.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by evilcubed View Post
    I have a set of commands in a function that i want to end in return 0;
    and i am using a void function.
    void functions by definition do not return a value. As was pointed out by heinz55 you can use return with no arguments to return early from the function.. if you want to return 0 why not make the function int instead?
    Code:
    int myfunc(int foo)
    {
        if (foo > 123)
            return 0;
        return -1;
    }
    to exit the program use exit()

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I believe you want to use 'exit'.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It's probably not a good idea to rely on exit though, since you will probably have leaked memory, unless you took care to register destroy functions with atexit.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    19
    hmmm exit; doesnt seem to work? i also tried exit(); but it gave me an error of too few arguments

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Any operating system worth its salt will release any memory and file handles your process has allocated when you call exit. Using atexit might be useful to clean up temporary files, for example. But if you look in the temp directories of most computers they tend to be littered with nonsense anyway.

    That being said, I think the real harm in using exit() is that it can result in a design flaw - if any function is allowed to "exit" then the main control of the program is lost. A better design allocates specific responsibilities to specific functions. Its like having a factory where any employee can just shut the whole thing down. Better that a manager is notified and then the manager decides that a shutdown is called for.

  10. #10
    Registered User
    Join Date
    Jun 2012
    Location
    Here
    Posts
    23
    Try exit(0); not just exit();

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by evilcubed View Post
    hmmm exit; doesnt seem to work? i also tried exit(); but it gave me an error of too few arguments
    It sounds like you need to discover "man pages" or some function reference. Try typing "man 3 exit" into Google

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by c99tutorial View Post
    Any operating system worth its salt will release any memory and file handles your process has allocated when you call exit. Using atexit might be useful to clean up temporary files, for example. But if you look in the temp directories of most computers they tend to be littered with nonsense anyway.
    exit does promise some things will happen including the release of temporary files (if you bothered to use mkstemp in the first place)... I'm not sure about the rest of it.

    That being said, I think the real harm in using exit() is that it can result in a design flaw - if any function is allowed to "exit" then the main control of the program is lost.
    This - QFT.

  13. #13
    Registered User
    Join Date
    Nov 2012
    Posts
    19
    Exit (0) works, thanks

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    15
    void functions don't return a value. But you can simulate a return value by passing in pointers and modifying them. If you just want to terminate the program then what the others have said should work.

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by evilcubed View Post
    hmmm exit; doesnt seem to work? i also tried exit(); but it gave me an error of too few arguments
    We simply gave you the name of the function. It's your responsibility to look up how to use it, as you probably should with any new thing you're introduced to.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. difference between void and static void function
    By mahaju in forum C++ Programming
    Replies: 7
    Last Post: 12-27-2011, 04:02 AM
  2. how to return in void function();
    By pczafer in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2009, 03:06 PM
  3. return something in a void
    By vrkiller in forum C++ Programming
    Replies: 11
    Last Post: 02-10-2009, 07:25 PM
  4. void return
    By l2u in forum C++ Programming
    Replies: 20
    Last Post: 12-15-2006, 11:55 AM
  5. Replies: 4
    Last Post: 01-02-2002, 10:57 PM