![]() |
III. Function Design Patterns |
There are three steps to successfully designing a function:
The overall program design determines the purpose of each individual
function. In Section II above we dealt with
algorithm development. In this section we consider patterns for
determining the receives and returns of functions. In some cases simple
algorithms are also included. It turns out that, in most cases, the
purpose of a function determines the rest. In other words, by matching the
patterns below with the purpose of a function you are designing, you can
save yourself a lot of work in designing your functions. And, you can feel
more secure that your design is correct. Note that the purpose statements below are very general. When designing
a function, your purpose statements should be more specific, for
example:
However, when you go to match the purpose statement of your function
with the purpose's listed here, ignore the extra details you have
provided. In other words, if your purpose statement includes the word
'input' or 'get', you probably should work with pattern A below. Likewise,
if your purpose statement includes the word 'calculation' it should
consider pattern C. Once you have finished designing your functions, you may want to use
the algorithmic patterns to help develop an
algorithm or write the code. This is especially true if the function is at
all complex. Simply try to match the purpose of the function with the
purpose statements provided with each algorithmic pattern.
Goal: The appropriate variable(s) contain the value(s) Receives: Nothing Returns the variable(s) Algorithm: Get the value(s) for the variable(s) from the
user
Goal: The screen displays the value(s) Receives: the value(s) Returns NONE Algorithm: Output the value(s)
Goal: Some variable holds the result of the calculation Receives: the value(s) needed for the calculation Returns: the value calculated Pattern D: Message Display
Goal: The message is displayed Receives: None Returns: None Algorithm: Display the message.
|