Menu

is return nessesary?

David
2007-12-05
2012-09-26
  • David

    David - 2007-12-05

    Ok, so I was wondering if you could just leave out the return 0; statement at the end of your main() if you use a menu for like a game that uses bool to check if the main should exit?

     
    • BiT

      BiT - 2007-12-05

      all you needed to do was type in the search box "Return main" and you would have found a lot of information on this.

      Why must people be so lazy...you learn nothing by being lazy

      http://sourceforge.net/forum/message.php?msg_id=2536760

       
    • David

      David - 2007-12-06

      yea well i did a search but i was a little more specific i wasn't so vague on my search i literally typed "is return necessary"

       
    • Anonymous

      Anonymous - 2007-12-06

      If you are executing a function and reaches the end of the main block without having found a return, control returns to the function that invoked, namely the ruling return is not strictly necessary in any function, unless otherwise want to return a value, or return control to the caller function before the end of the body that runs. If the caller function expect a value and the called comes to an end without having found an adequate return, the value received is rubbish.

      Note: There is one exception, the main function, which if reached the final without having found a return, the compiler automatically provides an adequate return int, since by definition, this function returns int.

       
    • Anonymous

      Anonymous - 2007-12-06

      What is 'lazy', is thinking that not typing "return 0" is such a major time saving! Who cares!? Just do it. The time you will 'save' probably in your entire life by avoiding this you have just spent discussing it on a forum! The time you spend fixing the bugs you introduce by poor programming practices will be much longer. ;-)

      In C, where main() is declared as returning an int, it must explicitly return a value. In C++ main() may only be declared as returning an int, but does not need an explicit return statement. This is true only of main() in C++, all other functions must have an explicit return statement.

      I suggest that for the sake of C/C++ portability, and even portability between ISO C++ and less compliant C++ compilers (for example VC++ v6 insists on an explicit return statment in main() even for C++), I suggest that you always have an explicit return.

       
    • David

      David - 2007-12-06

      Ok well old newbie I clicked the link and read it might not have explained it in detail but it did state that it is a standard to have it in there so thus I would have to put it in there, correct?

      And C i asked the question because i had written a program and put it in than removed it to see what would happen and nothing really did. So what i should have asked was if it would leave any bugs in a long term type of enviorment.

       
      • Anonymous

        Anonymous - 2007-12-07

        >> it did state that it is a standard to have it in there
        >> so thus I would have to put it in there, correct?

        Well, strictly it did not say that and without qualification is ambiguous w.r.t. the ISO standard, and your conclusion is incorrect. The standard is as I stated. To clarify:

        1) In ISO C, main() declared as returning an int must be supported, and other forms (such as void) are allowed but not required.

        2) In C++ the only allowed forms of main() are those that are declared as returning an int.

        3) In C, if main() is declared to return an int, there must be an explicit return statement that returns an int.

        4) In C++ main() need not have an explicit return statement even though it always returns and int. A default return value is supplied. This is true of main() only - no other functions.

        >> So what i should have asked was if it would leave any
        >> bugs in a long term type of enviorment.

        No, a return value if zero is supplied. By convention zero implies success. This is used by tools such as 'make' to determine if an error has occurred.

        Clifford

         
      • Osito

        Osito - 2007-12-06

        I've made console apps with and without the return and the only difference that I've seen is that you can get compiler warnings if you leave it out and that warning option enabled.

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.