+ 2

Why is "return 0" written in the end of main() function, and why not anything other than that to be returned?

Programming in C++

9th Mar 2018, 9:39 AM
Amlan Abhilash Mishra
Amlan Abhilash Mishra - avatar
8 Réponses
+ 20
@ Amlan Abhilash Mishra, you may write return 1, -1, -2 or maybe 3, do it and see what happen 👍😉
9th Mar 2018, 10:41 AM
tooselfish
tooselfish - avatar
+ 7
its a way to exit the main function, return 0 means that the main function terminated normaly.
9th Mar 2018, 9:50 AM
D_Stark
D_Stark - avatar
+ 6
0 means no error anything greater indicates an error 👍
9th Mar 2018, 9:59 AM
D_Stark
D_Stark - avatar
+ 4
Why not return 1?
9th Mar 2018, 9:54 AM
Amlan Abhilash Mishra
Amlan Abhilash Mishra - avatar
9th Mar 2018, 9:58 AM
᠌᠌Code X
᠌᠌Code X - avatar
+ 4
Then why not -1 or -2 which are smaller than 0?
9th Mar 2018, 10:02 AM
Amlan Abhilash Mishra
Amlan Abhilash Mishra - avatar
+ 1
the main function may return an integer and 0 is just a convention for sucess. Conventions are important in programming for maintainability. Imagine If every programmer decides his own way to return a sucessful function or the way to name variables and etc, the confusion to understand what is going on would be huge...
9th Mar 2018, 12:01 PM
Pietro Majowka
Pietro Majowka - avatar
+ 1
suppose you are making a game and lets say there are two types of coding scripts one for swinging swords and other for axe so if you dont put return in the end of the sword code then when you try to use your sword you will draw out your axe too so if you put return it terminates the sword code and switches to axe
15th Mar 2018, 7:25 AM
CodeGod
CodeGod - avatar