Section 9 - Procedures It is possible for one program to call another program. The program that is called from another is referred to as a procedure. There are two main reasons for using procedures. The first reason is to avoid rewriting the same code when the same thing must be done at several places in a program. For example, suppose you need to find the median of several sets of numbers. The process of finding the median is the same for each set, only the numbers change. It would therefor be more efficient to write a procedure to find the median of an arbitrary set of numbers and calling it each time it is needed than to include the code for the complete process at each point. Suppose a city is divided into quadrants. We have random samples of the cost (in thousands) of homes in each of the quadrants. The data for the north east quadrant is stored in a list called NE, for the north west quadrant in a list called NW, for the south west quadrant in a list called SW, and for the south east quadrant in a list called SE. (Recall that in HP terminology a "list" is a set of objects contained in braces, {}. Any type of objects can be contained in a list.) We wish to find the median cost of homes in each of the quadrants and in the city as a whole. The program MEDN below is a procedure to find the median of the numbers in a list on level 1 of the stack, and the program FMED calls that procedure 5 times to find the median of each quadrant and for the city as a whole. The program SM is a procedure to display each median. Enter the following three programs into your calculator. << NE MEDN "NE" SM NW MEDN "NW" SM SW MEDN "SW" SM SE MEDN "SE" SM NE NW + SW + SE + MEDN "CITY" SM >> Save the above program as FMED. NOTE: When two lists are "added" with +, they are concatenated into one list. The + sign can also be used to add an object to a list. If the list is on level 2 and an object is on level 1, the object will be added to the end of the list. If the object is on level 2 and the list on level 1, the object is added to the beginning of the list. See Chapter 8 of UG for more information about list manipulation commands. << DUP SIZE The CEIL function, (LS MTH F5-REAL NXT NXT F4-CEIL), is described on page 3-14 of UG. Save the above program as MEDN. << "Median for
Save the above program as SM. Now create the four lists of data shown below and save them with the name indicated. These files, and the file needed in Section 10, are on the Web. To download them, click here. NE = { 89 105 97 120 101 94 142 121 110 98 109 132 126 114 91 126 173 } NW = { 234 159 197 348 288 175 268 368 312 } SW = { 129 85 101 114 104 125 103 178 161 155 179 150 103 161 145 127 158 102 178 173 106 } SE = { 105 131 131 120 94 90 142 138 122 87 83 136 109 129 98 91 121 141 128 ) Now execute FMED and you should see the following results in message boxes: "MEDIAN FOR NE = 110" "MEDIAN FOR NW = 268" "MEDIAN FOR SW = 129" "MEDIAN FOR SE = 121" "MEDIAN FOR CITY = 126" NOTE: The sample sizes for the quadrants must be proportional to the number of homes in each quadrant for the CITY median to be valid. If you do not get the correct answers, use the techniques discussed in
Section 3 to try to correct the error. It is usually best
to use the bottom up testing technique. That is, it is best to test the lowest level procedures before the calling programs. To
test SM, put a number on level 2 of the stack and a name in quotes on level one. Press the SM button and see if you get the
right output. Next put a list of numbers on level 1 of the stack and press MEDN to see if the correct median is being
computed. Be sure to test with both an even and with an odd number of data points in the list. Finally, check FMED. If
you are using the single step procedure described in Section 3,
note that in the RUN directory there is a command called
SST and one called Notice that the procedures MEDN and SM are very different in nature. MEDN is a general purpose procedure that can easily be used as a stand alone program or as a procedure for another program. One can manually put a list of numbers on the stack and press the MEDN command or, as in the example above, have a program place a list on the stack and call MEDN. In either case, MEDN will find the median and leave it on the stack ready to be used as needed. On the other hand, SM is a special purpose procedure intended specifically for FMED to display its results. It could, of course be used by other programs, but it was written with this particular program in mind. Clearly the coding for SM could have been included in MEDN, which would have made FMED shorter since it would have required only one procedure call for each data set, but it would have negated the generality of MEDN. This is a tradeoff that the intelligent programmer must always keep in mind. In this case it seemed more reasonable to keep MEDN general since it could have many other applications with the SM coding left out. The other reason for using procedures is to make long programs easier to write, understand, and test. In the classical top down design approach to programming, a large problem is broken down into smaller parts called modules. Each module is then broken down into smaller modules until each module in a relatively simple problem. Then each module is coded as a special purpose procedure. For example, a payroll program may be broken down into modules to compute the gross pay, compute deductions, update year to date totals, and print a check. The gross pay module may have separate modules to compute regular pay and overtime pay. The deductions module could contain separate modules to compute federal taxes, state taxes, local taxes, insurances, 401K's, etc. We will not code this example as it would take too much time and space, but if you have a long program to write, this approach is highly recommended. The logic of the program is much easier to follow and the bottom up testing technique discussed above makes it much easier to find errors. EXERCISE SET 9 1. Given two sides a and b and the included angle C of a triangle, the third side, c, can be found with the law of cosines
|