+1

In the course we always use return 0; what does it mean? And can we use another number? What does these numbers exactly do?

2/17/2017 7:53:34 AM

mohammad mazhariroshan

10 Answers

New Answer

+13

From Bjarne Strostrup book: First it will output to the screen, and then it will return a value 0 to whoever called it. Since main() is called by system, we won't use that return value. However on systems like Unix/Linux it can be used to check whether the program succeeded. A zero returned by main() indicates the program terminated successfully.

+5

yes, the keyword return ends the fuction it's in and if the function has a return type, like the main function usually does, a value is also returned. For the return value of main, that is what the program itself returns when the main function is finished. A return value of zero generally means that the program ran successfully and everything went smooth.

+5

RETURN- end current function and returns specified value to the caller function. return 0- means that its back to the top ( at the begin ) or in other words If control reaches the end of the main function, return 0; is executed.

+4

Exit codes are captured by the calling process in a special, automation-desirable way. In interactive code, they're stored in these variables (examples; there are others): $? : Linux, Android and OSX $? and $LASTEXITCODE : Windows Powershell To show they're widely used (Windows example): https://msdn.microsoft.com/en-us/library/windows/desktop/ms681381(v=vs.85).aspx Getting Windows / Python exit code text: C:\>net helpmsg 5 / print(os.strerror(42)) Access is denied. / Illegal byte sequence. Notes: 'Standard' implies you could use the codes too. Negative codes may cause unintended behavior. void main() is explicitly prohibited; some compilers will substitute 0. "Always 0" confounds automation unless your program can never fail.

+3

If your main method returns zero, the operating system receives this value from the program. In general, this means that execution completed properly. While returning another value is allowed by the compiler, it is conveniently incorrect, as 0 is the universal return value to denote successful execution. Return something else to denote that execution did not complete successfully.

+2

when creating your own main function, you'll have a list of numbers other than 0, which will indicate the error. e.g., if the execution returned -1, it's an error while creating a list etc. by returning 0, you imply it all worked out. you can see the returned number in terminal/cmd

+2

In C++ the return type of any method is int by default. So you have to return something that is why you return 0. But if your function does want to return something else like sum of numbers etc then you can return that as well.

+1

Return is one of those things that has an extremely important role. In your int main, you'll use it to return a numeric run status to the operating system (in general, 0 = run complete without errors). More importantly, like that 0 is returned to its caller (the OS), returns are used throughout almost everything you do in your code... Something like the Pythagorean theory is one of my favorite examples. A^2 + B^2 = C^2 int side1=3; int side2=4; int side3=PythagoreanTheory(side1, side2); Elsewhere, method "PythagoreanTheory" is declared... int PythagoreanTheory(int firstSide, int secondSide) { int aSquare=firstSide*firstSide; int bSquare=secondSide*secondSide; return sqrt(aSquare+bSquare); } This return calls a method named sqrt... sqrt will find the square root and return it to its caller, in this case it would return "5" (square root of 9+16), which the current method would set as its return value and in turn send that back to the first caller! "int side3=PythagoreanTheory(side1, side2);" above completes the function on the right to establish side3's value, so after returning 5, this original call becomes "int side3=5", which then sets the value of side3 as 5 :) This is, of course, a very simplified example (and sqrt requires you include the correct file), but returns are used in a similar way in every object oriented language and as shown here, does not only get used to send something to the OS; in fact, most returns are indeed internal! Most importantly, a return does NOT have to an int... It can be string, float, etc, or if you want the method to perform a function and return NOTHING, then we use void :) Also note that I used int because 3,4,5 works out that way, but technically it would be a float for more accuracy :P

+1

return 0 is used in"int main()" function.it asks the function to return an integer value. we can use any integer instead of 0.ex- return 1;

+1

It returns 0 to the operating system, which tells the compiler that, that's the end of program execution. Failure to bring it will results in error; I think syntax error