A good plan of attack for designing the algorithm for a program is to break down the task to be accomplished into a few subtasks, then decompose each subtask into smaller subtasks, and so forth until the subtasks are simple enough that they can easily be implemented as C++ code. This approach is called top-down design.
A function that returns a value is like a small program. The arguments to the function serve as the input to this “small program” and the value returned is like the output of the “small program.”
When a subtask for a program takes some values as input and produces a single value as its only result, then that subtask can be implemented as a function.
A function should be defined so that it can be used as a black box. The programmer who uses the function should not need to know any details about how the function is coded. All the programmer should need to know is the function declaration and the accompanying comment that describes the value returned. This rule is sometimes called the principle of procedural abstraction.
A variable that is declared in a function definition is said to be local to the function.
Global named constants are declared using the const
modifier. Declarations for global named constants are normally placed at the start of a program after the include
directives and before the function declarations.
Call-by-value formal parameters (which are the only kind of formal parameter discussed in this chapter) are variables that are local to the function. Occasionally, it is useful to use a formal parameter as a local variable.
When you have two or more function definitions for the same function name, that is called overloading the function name. When you overload a function name, the function definitions must have different numbers of formal parameters or some formal parameters of different types.