III. Function Design Patterns


Algorithmic Patterns Main Menu Class Design Patterns

There are three steps to successfully designing a function:

  1. defining the purpose of the function;
  2. deciding on the receives and returns;
  3. developing an algorithm.

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:

  • To input the cost of an item in the inventory.

  • To output the discounted price;

  • To calculate an employee's wage.

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.
 

Pattern A: Basic Input

    Purpose: To input or get some value(s) from a user

    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
                    // Consider Using algorithmic pattern A1.


Pattern B: Basic Output

    Purpose: To output some value(s)

    Goal: The screen displays the value(s)

    Receives: the value(s)

    Returns NONE

    Algorithm: Output the value(s)


Pattern C: Calculations

    Purpose: To perform some calculation

    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

    Purpose: To output a message

    Goal: The message is displayed

    Receives: None

    Returns: None

    Algorithm: Display the message.



Top of Section Main Menu Class Design Patterns