|
|
|
|
|
|
|
in the second version. With this philosophy, control enters a function at one point only (the first executable statement) and exits at one point only (the end of the body). They argue that multiple exits from a function make the program logic hard to follow and difficult to debug. Other programmers take a position somewhere between these two philosophies, allowing occasional use of the return statement when the logic is clear. Our advice is to use return sparingly; overuse can lead to confusing code. |
|
|
|
| | |
|
|
|
|
When you choose a name for a void function, keep in mind how calls to it will look. A call is written as a statement; therefore, it should sound like a command or an instruction to the computer. For this reason, it is a good idea to choose a name that is an imperative verb or has an imperative verb as part of it. (In English, an imperative verb is one representing a command: Listen! Look! Do something!) For example, the statement |
|
|
|
| | |
|
|
|
|
has no verb to suggest that it's a command. Adding the verb Print makes the name sound like an action: |
|
|
|
| | |
|
|
|
|
When you are picking a name for a void function, write down sample calls with different names until you come up with one that sounds like a command to the computer. |
|
|
|
|
|
|
|
|
|
From the very beginning we have been using #include directives, requesting the C++ preprocessor to insert the contents of header (.h) files into our programs: |
|
|
|
|
|
|
|
|
#include <iostream.h>
#include <math.h> // For sqrt() and fabs()
#include <fstream.h> // For file I/O
#include <limits.h> // For INT_MAX and INT_MIN |
|
|
|
|
|