5.9. Returning from main

Now that we have functions that return values, I can let you in on a secret. main is not really supposed to be a void function. It’s supposed to return an integer:

int main () {
  return 0;
}

The usual return value from main is 0, which indicates that the program succeeded at whatever it was supposed to do. If something goes wrong, it is common to return -1, or some other value that indicates what kind of error occurred.

Of course, you might wonder who this value gets returned to, since we never call main ourselves. It turns out that when the system executes a program, it starts by calling main in pretty much the same way it calls all the other functions.

There are even some parameters that are passed to main by the system, but we are not going to deal with them for a little while.

Before you keep reading...

Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.

You have attempted 1 of 4 activities on this page