Each C + + program contains one or more functions (function), one of which must be named main.
Note : the operating system runs a C + + program by calling Main, where the main function is the entry for program execution , so each program must have a function named Main.
Here is a very simple main function, which does nothing but returns a value to the operating system:
1 int Main () {2 return 0 ; 3 }
The definition of a function consists of four parts : return type , function name, and a list of formal parameters surrounded by parentheses (parameter list, allowed to be empty ), function body (functions body).
Note: The return type of the main function must be int, which is the integer type.
The last part of the function definition is the function body, which is the block of statements (block of statements), beginning with the left curly brace (curly brace), ending with a closing curly brace :
1 {2 return 0 ; 3 }
Return, which ends the execution of the function. when a return statement includes a value, the type of the return value must be the same as the return type of the function . In this case, the return type of main is int, and the return value of 0 is indeed a value of type int.
extension: If the function does not need a return value, only void is used. If you need to end the function with a return, you do not need to follow the argument after return. Example: // There is a return type int getsum (int A, int b) { return a + b;
} // no return type void Myprint () {std::cout << this is test " << Std::endl; return ;//This return statement may not be written.
}
In most systems, the return value of main is used to indicate the state. A return value of 0 indicates success, and the meaning of a return value other than 0 is defined by the system and is typically used to indicate the type of error.
Notice that the semicolon at the end of the return statement. In C + +, most C + + statements end with a semicolon. They are easy to ignore, and if you forget to write a semicolon, it can lead to a confusing compilation error.
Important concept: Type A type defines not only the contents of the data elements, but also the operations that can be performed on such data. The data processed by the program is stored in variables, and each variable is unique to its own type. If the type of a variable named V is T, we usually say "V has type T", or equivalent, "V is a variable of type T".
(original) C++primer (fifth edition)--1.1 a simple C + + program