< previous page page_23 next page >

Page 23
Using Comments
Writing comments well is a skill few programmers master.
It is best to assume your audience can read C++, but can't read your mind. Let the source code tell what you are doing, and use comments to explain why you are doing it.
Functions
While main() is a function, it is an unusual one. Your operating system invokes main() to start your program. Other functions are called, or invoked, from main() or from one another during the course of your program.
The function main() always returns an int, which is a C++ term for integer. As you'll see in the coming hours, other functions might return other types of values or might return nothing at all.
A program is executed line by line in the order it appears in your source code, until a function is called. Then the program branches off to execute the function. When the function finishes, it returns control to the next line in the calling function.
Imagine that you are drawing a picture of yourself. You draw the head, the eyes, the nose, and suddenly your pencil breaks. You branch off to the sharpen my pencil function. That is, you stop drawing, get up, walk to the sharpener, sharpen the pencil, and then return to what you were doing, picking up where you left off. (I think you were adding a cleft to your chin.)
When a program needs a service performed, it calls a function to perform the service. When the function returns, the program resumes where it was just before the function was called.
Listing 2.3 demonstrates this idea.
LISTING 2.3 DEMONSTRATING A CALL TO A FUNCTION

d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
1:     #include <iostream.h>
2:
3:     // function Demonstration Function
4:     // prints out a useful message
5:     void DemonstrationFunction()
6:     {
7:         cout << In Demonstration Function\n;
8:     }
9:

 
< previous page page_23 next page >

If you like this book, buy it!