Testing User-defined Functions
Testing is an often overlooked but critical step in programming user-defined functions. Many functions are fairly straightforward implementations of formulas you are given. However, it is important to test each function that you define to ensure that it produces the correct result before you use it to solve a problem.
If your function calculates a value like the FtoC
function we defined in a previous lesson than you can test its correctness on
some known values. We know that 212F is equal to 100C and that 32F=0C.
So we can call (use) our FtoC
function as follows from the
Command Window and ensure that the results are what we expect.
FtoC(212)
FtoC(32)
FtoC(70)
We could also try a couple of values that are very low, 15F, -10F, and some that are high 250F and use a thermometer to check the accuracy within some range we are interested in. If we are developing the formula for such a conversion, we would want to test several different known boundary conditions. However, we chose a well-known temperature conversion for our example and testing it on a few values should be enough to convince you that you have programmed your function correctly.
In later lessons, we will use selection and repetition statements to implement more complex algorithms in our functions. You will learn to test for and fix common bugs (errors) in complex code. For now, be sure to test each function that you define before using it as part of a larger solution to a problem.