< previous page page_334 next page >

Page 334
deal of effort in coding many problem solutions. If there is a task that must be done in more than one place in a program, we can avoid repetitive coding by writing it as a function and then calling it wherever we need it. Another example that illustrates this use of functions appears in the Problem-Solving Case Study at the end of this chapter.
If more than one parameter is passed to a function, the formal and actual parameters are matched by their relative positions in the two parameter lists. For example, if you want PrintLines to print lines consisting of any selected character, not only asterisks, you might rewrite the function so that its heading is
void PrintLines( int numLines,
                 char whichChar )
and a call to the function might look like this:
PrintLines(3, #);
The first actual parameter, 3, is matched with numLines because numLines is the first formal parameter. Likewise, the second actual parameter, #, is matched with the second formal parameter, whichChar.
Syntax and Semantics of Void Functions
Function Call (Invocation)
To call (or invoke) a void function, we use its name as a statement, with the actual parameters in parentheses following the name. A function call in a program results in the execution of the body of the called function. This is the syntax template of a function call to a void function:
0334-01.gif

 
< previous page page_334 next page >