Section 7h

Programming by Contract - Post Conditions


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Specifying the Function Outcomes

Post Conditions

The next step is to figure out what should have happened when the function has completed its mission. This is not any more difficult than preconditions. Basically, you figure out what the resulting value to be returned (if any) should be.

For the previous examples, divideTwoNumbers should return the quotient result of the divided numbers, calcFactorial should return a factorial, and calcTotal should calculate a tax from the subtotal, add the tax to the subtotal, and then return it. Your postconditions for all normal circumstances should simply be the reason you wrote the function.

Note that some functions that conduct I/O will rarely if ever return a result. However, the postconditions for this kind of function will be that a certain thing is displayed on the screen, some data is output to a file, and so on.

This is also a good place to note the difference between processing and I/O functions. A processing function is designed to take in some data, conduct some kind of processing such as math or decision making, and return a result to the calling function. An I/O function is a function that delivers data to a device. Your I/O functions deliver data from your program to the monitor as did your printString or printChar functions. Your processing functions calculated the roots of a quadratic equation or the remaining side of a right triangle and returned their results to their calling functions.

Processing and I/O functions must not be combined. It is very common to see functions that calculate something and then output the result on the Internet and in many textbooks. This represents very poor -- in fact, virtually nonexistant -- modularity, and obviously very poor software development. If you conduct output within a processing module, you will almost certainly have to go back and change the appearance of the output, or even change the tools of the output if you used a particular C I/O utility, the formatted command line I/O tools, or any of a variety of other output operations. Again, this means the function lacks the modulariy required to lift it out of your program and drop it into someone else's program who needs the kind of processing conducted in the function. Again, do not combine I/O and processing operations in the same function.

Like the preconditions, once you know what the postconditions are, you can decide on (and specify) the output of the function. Generally, the output of the function is the value that the function returns from itself to its calling function. However, other output processes may involve display on the screen, output to a file, or other output actions, as mentioned previously.

When you begin work with the Six Step Programming Process, you will select what kind of output the function has, whether it is to return some processed quantity to the calling function, or to send some data to a device. In any event, the reason to specify post conditions is so you can verify that your function does in fact do what it was meant to do. This is not a long or complicated section, and identifying post conditions is not difficult. However, as you will find, it will be critical to your program development.