|
|
|
|
|
|
|
In general, you can code any module as a function, although some are so simple that this really is unnecessary. Thus, in designing a program, we frequently need to decide which modules should be implemented as functions. The decision should be based on whether the overall program is easier to understand as a result. There are other factors that can affect this decision, but for now this is the simplest heuristic (strategy) to use. |
|
|
|
|
|
|
|
|
If a module is a single line only, it is usually best to write it directly in the program. Turning it into a function only complicates the overall program, which defeats the purpose of using subprograms. On the other hand, if a module is many lines long, it is easier to understand the program if the module is turned into a function. |
|
|
|
|
|
|
|
|
Keep in mind that whether you choose to code a module as a function or not affects only the readability of the program and may make it more or less convenient to change the program later. Your choice does not affect the correct functioning of the program. |
|
|
|
|
|
|
|
|
Writing Modules as Void Functions |
|
|
|
|
|
|
|
|
It is quite simple to turn a module into a void function in C++. Basically, a void function looks like the main function except that the function heading uses void rather than int as the data type. Additionally, a void function does not use a statement like |
|
|
|
|
|
|
|
|
as does main. A void function does not return a function value to its caller. |
|
|
|
|
|
|
|
|
Let's look at a program using void functions. A friend of yours is returning from a long trip, and you want to write a program that prints the following message: |
|
|
|
|
|
|
|
|
***************
***************
Welcome Home!
***************
***************
***************
*************** |
|
|
|
|
|
|
|
|
Here is a design for the program. |
|
|
|
|
|