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
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
In void function just write 'return'
Code:void someFunction(void) { /* some code here */ return; }
Sorry I must've misunderstood you. Do you mean exit the entire program? Or just exit the 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?
to exit the program use exit()Code:int myfunc(int foo) { if (foo > 123) return 0; return -1; }
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"
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.
hmmm exit; doesnt seem to work? i also tried exit(); but it gave me an error of too few arguments
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.
Try exit(0); not just exit();
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.
This - QFT.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.
Exit (0) works, thanks
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.
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"