A function either returns a value (has a return type) or not (its return type is void
). A function that has a return type must return a value. A function whose return type is void
mustn't return a value.
The main
function is a special function. It's the starting point of your program and it returns an int
(has a return type of int
).
By convention, we return 0 from main
upon successful exit or a value that maybe represents an error code otherwise. So, technically, we're NOT restricted to return a specific value from main
, but we ARE restricted to return an int
value (although even this, the compiler can cover it and return an int
value for us).
The value returned from main
is useful in a sense that other programs can use it. We often think of the value that's returned from main
as the exit status of our program. So, again by convention, when main
returns 0, we chose to think of this as a successful termination of the program (no errors).
Since an int
value can be any of (2^32 - 1) different values, we have exactly this number of options to return one of them where each one can represent a different status (for example, I may choose to return 1 from main
in case the correct number of command-line arguments isn't provided and so on).
For more information about
- functions
- command-line arguments