|
|
|
|
|
|
|
The second item to be printed is the value obtained by calling (invoking) the Square function, with the value 27 as the number to be squared. As the servant, the Square function performs its task of squaring the number and sending the computed result (729) back to its caller, the main function. Now main can continue executing by printing the value 729 and proceeding to its next statement. |
|
|
|
|
|
|
|
|
In a similar fashion, the second statement in main prints the message |
|
|
|
|
|
|
|
|
and then invokes the Cube function and prints the result, 19683. The complete output produced by executing this program is, therefore, |
|
|
|
|
|
|
|
|
The square of 27 is 729
and the cube of 27 is 19683 |
|
|
|
|
|
|
|
|
Both Square and Cube are examples of value-returning functions. A valuereturning function returns a single value to its caller. The word int at the beginning of the first line of the Square function |
|
|
|
|
|
|
|
|
states that the function returns an integer value. |
|
|
|
|
|
|
|
|
Now look at the main function again. You'll see that the first line of the function is |
|
|
|
|
|
|
|
|
The word int indicates that main is a value-returning function that should return an integer value. And it does. After printing the square and cube of 27, main executes the statement |
|
|
|
|
|
|
|
|
to return the value 0 to its caller. But who calls the main function? The answer is: the computer's operating system. |
|
|
|
|
|
|
|
|
When you work with C++ programs, the operating system is considered to be the caller of the main function. The operating system expects main to re- |
|
|
|
|
|