Template for Function Design

Please use this template for each of the functions you are designing for your program.

For "preconditions" above, write more than just "four parameters" or "2 parameters". You should write what type each one is, what it means, and, if it is not clear, why that parameter is being sent to this function. You should state any assumptions that the function will be making about the parameters ("size is always positive", for example).

Post conditions should include the "result" of the function. If more than one result is returned, they should be listed in the order they are returned. If the function does any kind of actual output, that should be described also. It could be to the screen, a graphics window, or a file. If the function makes a change in any parameter that is mutable (reference), that should be documented. If it is possible that this happens (not certain) describe the circumstances when it could happen. "The list of employees will be changed if the removal is successful."

Changes to files (opening, closing, reading from, writing to) need to be mentioned as a side effect of the function.

Design itself will be short or long depending on the job of the function. It could be one line if the function simply calculates something and returns it. It could be a screenful if it is a complex function. This is where you show the control structures that you are using.

Example: a function that will find the total of numbers in a file.
FindTotal
preconditions (parameters): name of file
assumes numbers are one per line
purpose: to find the total of all numbers in the given file
postconditions: returns the total
will be zero if the file is empty. File will be closed at end of function.
Design
  • open file
  • initialize accumulator (total)
  • for all the lines in the file
  •     add the value of the line to the total
  • close file
  • return total

Some people like to use lines like
#*****************************************
or
#-------------------------------------------
between the function designs, to make it easier to find the different functions.