+ 2

How Does the "Return 0" Statement actually work?

I have attempted to change the Value of the "Return" statement hoping to see a difference of some kind. but i was unable to notice anything. Can someone better explain the purpose of changing the value, and how it actually applies to coding cause i'm a little confused on what changing the value from "0" does. Thank you!

2nd Aug 2017, 9:23 PM
Morris House III
Morris House III - avatar
9 Antworten
+ 10
try it on your pc. with visual studio return -1 causes an error to be reported once your exe has exited
2nd Aug 2017, 9:49 PM
jay
jay - avatar
+ 4
Linux/OSX/Android: $ echo "hi there" | grep "hi" hi there $ echo $? 0 $ echo "hi there" | grep "fail" $ echo $? 1 $? is where return codes appear in interactive shells (and bash scripts, etc). On Linux-y systems, 0 is success. Windows is goofy ... in consoles, return codes are stored in %ERRORLEVEL% but you have to check by thresholds (PowerShell is much better in this regard). On Windows systems, to explain codes: C:\>net helpmsg errorcode Outside of consoles, a special field is used in 'interprocess communication'. You can see these with Microsoft's advanced system internals utilities: https://docs.microsoft.com/en-us/sysinternals/
3rd Aug 2017, 5:47 AM
Kirk Schafer
Kirk Schafer - avatar
+ 4
@Jamie: I don't have Windows at the moment; I've just worked in gigantic networks + remember a lot. For "Windows questions" I'll use WINE, SoloLearn's servers or browserling (Windows browsers and file manager there). Mostly I'm answering to demonstrate the concept applies to all OS's. For Windows devs those tools are quick + specific... ...vs a TL;DR for Android/Linux/OSX.
4th Aug 2017, 12:34 AM
Kirk Schafer
Kirk Schafer - avatar
+ 3
In C++ since standardization, main() *must* return a value. A compiler that doesn't at least complain is either very old or misconfigured (ie set to pre-C++98, or suppress prompts). Anyway that's the way it's supposed to be. OP, to answer your question, the return value of a program is typically used to inform the calling process of information such as: *. Common system error codes. *. App-specific exit statuses. *. Success or failure re the above. Example 1: You write a system software, you'll need to use the target platform's exit codes to indicate various errors, example: device_not_ready. Example 2: You write a software, say a game, and your game requires certain minimum screen resolution be 720p, it may have to exit with: inadequate_resolution. In the above scenarios, it's easier, especially in the first to use an exit code since the calling process can receive the number and then act accordingly. This also becomes important in chaining. Conversely, it's as important for the caller to know that there were no errors when the process terminates. This has been standardized across all platforms as a value of 0. This is why we "return 0;" at the end of our program. Because later we'll be adding early exit conditions and standard or implementation-specific exit codes to help callers handle them or users debug them. I hope this clears things up without getting too technical. What would you normally see? Nothing. Even an exit code won't normally show anything unless the console it's run from is told to display the return value (which is the problem using many IDEs). For example, some consoles may display "Application exited with status 0" or similar. So the caller's environmental settings are important and the "console" (it's not a proper one) in SL is not set to display this.
2nd Aug 2017, 11:37 PM
Jamie
Jamie - avatar
+ 3
@Kirk: Wow, you can use an MS stuff? What strange arcane power this is!
3rd Aug 2017, 11:22 PM
Jamie
Jamie - avatar
+ 2
The compiler's code calls your main function. It then stores what you return, and after the compiler's code is done cleaning up / checking stuff, it calls exit with the code you returned. The code your program exits with can be seen by debuggers, the OS, and the parent process
2nd Aug 2017, 11:29 PM
aklex
aklex - avatar
0
Well, since the access point of the program, the main method must return an integer, yes, it is necessary to return an integer, zero indicates that the program ended successfully. When your program ends it shows you in the console the return, if your program crashed the number will be different from zero, and each number has an error message associated for debugging purposes
1st Feb 2019, 11:13 AM
AnkitKunwar
AnkitKunwar - avatar