Sign in to follow this  

Untitled

This topic is 4549 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to create a function that ends a program, but if I create one of the void type it won't let me use return 0, because it's of type void. How should I do it?

Share this post


Link to post
Share on other sites
Quote:
Original post by MSalley
I want to create a function that ends a program, but if I create one of the void type it won't let me use return 0, because it's of type void. How should I do it?


There are ways to do this, but I feel you're taking the wrong approach. The simplest way to do that:


bool closeApplication = false;

int main() {
while(!closeApplication() {
doStuff();
}

return 0;
}


void functions are used when a return value is not necessary. They do not end execution.

Share this post


Link to post
Share on other sites
I agree with xMcBaiNx, it's easiest to exit from the main-function by simply returning the value 0 with the return-function. If one really wants to end the program elsewhere though there's a standard function which terminates the program:
exit(0); //return the value 0 indicating it's a normal exit
If an error occured one can return the value 1 instead:
exit(1);

Example program:


#include <iostream>

void endProgram(); //end-function prototype

int main()
{
endProgram();

std::cout <<"This is a normal exit.\n";
return 0; //return 0 - normal exit
}

//end-function definition
void endProgram()
{
//any cleaning up could be done here such as deleting allocated memory:

std::cout <<"This is an exit by calling the exit function.\n";
exit(0); //0 - normal exit
}



\Jimmy H

Share this post


Link to post
Share on other sites
It's also OK* to end a void main() with simply return; (no value).






*I don't recommend or advocate writing void main(). Ever. int main(int argc, char** argv) is so natural to type.

Share this post


Link to post
Share on other sites
Quote:
Original post by Boku San
It's also OK* to end a void main() with simply return; (no value).


No it's not, because void main() is never OK to type (it was allowed in C89 IIRC, but it's not legal in C++ (and I think it might not be legal C99 either)). Recent compilers will give you an error message.

There are two legal forms for main:

1) int main () / int main ( void ) //these mean the same thing... return an int, accept no arguments
2) int main ( int argc , char * argv [] )

main is a very weird function in that:

1) You can't overload it. You're not allowed to.
2) If it reaches the end of the function without a return statement, it's assumed to return 0, rather than invoking undefined behavior. That is, this is legal and has defined behavior:
#include <iostream>
using namespace std;
int main () {
cout << "I like candy" << endl;
//no return statement.
}

Share this post


Link to post
Share on other sites
Well, this is what I think you're trying to do (I might be totally wrong, but...): you're trying to make a function like this:

void exitProgram()
{
// This is supposed to exit the program!
return 0;
}

int main()
{
// Do stuff

exitProgram();
}


Right? In that case, this doesn't work because 'return 0;' is not a universal statement to end the program. Instead, the 'return' statement just exits out of the function you put it in. Therefore, putting return; in exitProgram() makes the program exit exitProgram() but not main(). When you exit main() (with the return statement), the program ends.

If you want a function to exit the program, try exit(). You can use it like this:

int main()
{
exit( 0 ); // Instead of return 0
}


You need to include <cstdlib.h> I think.

Hope that helps!

Share this post


Link to post
Share on other sites
Pretty much, but I'm calling exitProgram() from another function(not main.) I used the exit(0) thing, It's cool. Thanks everyone for the help!!! Out.

Share this post


Link to post
Share on other sites
Quote:
Original post by fyhuang

int main()
{
exit( 0 ); // Instead of return 0
}



You can also use

#include <cstdlib>

exit(EXIT_SUCCESS);
// or
exit(EXIT_FAILURE);


Share this post


Link to post
Share on other sites
Quote:
Original post by MaulingMonkey
Quote:
Original post by Boku San
It's also OK* to end a void main() with simply return; (no value).


No it's not, because void main() is never OK to type (it was allowed in C89 IIRC, but it's not legal in C++ (and I think it might not be legal C99 either)). Recent compilers will give you an error message.

There are two legal forms for main:

1) int main () / int main ( void ) //these mean the same thing... return an int, accept no arguments
2) int main ( int argc , char * argv [] )

main is a very weird function in that:

1) You can't overload it. You're not allowed to.
2) If it reaches the end of the function without a return statement, it's assumed to return 0, rather than invoking undefined behavior. That is, this is legal and has defined behavior:
#include <iostream>
using namespace std;
int main () {
cout << "I like candy" << endl;
//no return statement.
}


Ouch...you sortof got me there. I haven't seen void main() in a long, long time. This is why.

EDIT: Just realized that people are lazy.
Quote:
The ISO C++ Standard (ISO/IEC 14882:1998) specifically requires main to return int. But the ISO C Standard (ISO/IEC 9899:1999) actually does not. This comes as a surprise to many people. But despite what many documents say, including the Usenet comp.lang.c FAQ document (at great length), the actual text of the C Standard allows for main returning other types.

Share this post


Link to post
Share on other sites
I think at this moment... its not necessary to have a function that ends a program... just make sure the flow control of the program is correct, and the program will exit where you want it to... I dunno, I've never had a function that explicitly exits the program... the only ones i, and most people have used are function routines that releases used memory and other stuff before exiting...

Share this post


Link to post
Share on other sites
Quote:
Original post by MSalley
I want to create a function that ends a program, but if I create one of the void type it won't let me use return 0, because it's of type void. How should I do it?


There is absolutely nothing magic about returning 0 that causes the termination of a program.* The magic is about returning from the main function.

Usually, the best way to terminate your program is to arrange for everything to return, all the way back to main, and to the end. If you are having difficulty seeing how to do this, there is likely something wrong with your code design.

However, other solutions exist, such as the standard library function exit() from <cstdlib>, as you have been shown. In C++ you could directly call std::terminate(), or deliberately throw an exception that isn't caught anywhere, but these are even uglier ways of doing it.




* The value being returned, however - whether or not it is 0, and you are technically allowed to return any integer - may be used for some magical purposes. In particular, it is used to tell scripts that use your program about what happened: by convention a 0 return means "the program finished successfully", and anything else is an error code. Breaking this convention gains you nothing and may lose you something (the ability to automate a run of your program smoothly), so please hold to it.

Note that a "return 0;" statement is not required; as a special case, it is implicit at the end of the main() function. My preferred style is to omit it, because it isn't needed and doesn't really communicate anything.

Share this post


Link to post
Share on other sites

This topic is 4549 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this