+ 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++
8 Réponses
+ 20
@ Amlan Abhilash Mishra, you may write return 1, -1, -2 or maybe 3, do it and see what happen 👍😉
+ 7
its a way to exit the main function, return 0 means that the main function terminated normaly.
+ 6
0 means no error anything greater indicates an error 👍
+ 4
Why not return 1?
+ 4
Then why not -1 or -2 which are smaller than 0?
+ 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...
+ 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