|
|
|
|
|
|
|
Every C++ program must have a function named main. Execution of the program always begins with the main function. You can think of main as the master and the other functions as the servants. When main wants the function Square. to perform a task, main Calls (or invokes) Square. When the Square function completes execution of its statements, it obediently returns control to the master, main, so the master can continue executing. |
|
|
|
|
|
|
|
|
Let's look at an example of a C++ program with three functions: main, Square, and Cube. Don't be too concerned with the details in the programjust observe its overall look and structure. |
|
|
|
|
|
|
|
|
#include <iostream.h>
int Square( int );
int Cube ( int );
int main()
{
cout < "The square of 27 is " < Square(27) < endl;
cout < "and the cube of 27 is " < Cube(27) < endl;
return 0;
}
int Square( int n )
{
return n * n;
}
int Cube( int n )
{
return n * n * n;
} |
|
|
|
|
|
|
|
|
In each of the three functions, the left brace ( { ) and right brace ( } ) mark the beginning and end of the statements to be executed. Statements appearing between the braces are known as the body of the function. |
|
|
|
|
|
|
|
|
Execution of a program always begins with the first statement of the main function. In our program, the first statement is |
|
|
|
|
|
|
|
|
cout < "The square of 27 is " < Square(27) < endl; |
|
|
|
|
|
|
|
|
This is an output statement that causes information to be printed on the computer's display screen. You will learn how to construct output statements like this later in the chapter. Briefly, this statement prints two items. The first is the message |
|
|
|
|
|