Learn the Importance of Main Function in Programming

Started by Kalyan, Dec 17, 2008, 03:18 PM

Previous topic - Next topic

Kalyan

Learn the Importance of Main Function in Programming

Function main() is the application's main routine where a program starts execution.It is the first user-written function run when a program starts.Object-oriented C++ programs consist mostly of classes, but there's always at least one C-like function: main(). main() is called more or less at the beginning of the program's execution, and when main() ends, the runtime system shuts down the program. main() always returns an int, as shown below:

int main() {
// ...Your Code goes here...
}
main() has a special feature: There's an implicit return 0; at the end. Thus if the flow of control simply falls off the end of main(), the value 0 is implicitly returned to the operating system. Most operating systems interpret a return value of 0 to mean "program completed successfully."

main() is the only function that has an implicit return 0; at the end. All other routines that return an int must have an explicit return statement that returns the appropriate int value.

Note that this example shows main() without any parameters. However, main() can optionally declare parameters so that it can access the command line arguments, just as in C.