< previous page page_405 next page >

Page 405
middle of a semester. The amount to be refunded is the total tuition times the remaining fraction of the semester (the number of days remaining divided by the total number of days in the semester). The people who use the program want to be able to enter the dates on which the semester begins and ends and the date of withdrawal, and they want the program to calculate the fraction of the semester that remains.
Because each semester begins and ends within one calendar year, we can calculate the number of days in a period by determining the day number of each date and subtracting the starting day number from the ending day number. The day number is the number associated with each day of the year if you count sequentially from January 1. December 31 has the day number 365, except in leap years, when it is 366. For example, if a semester begins on 1/3/97 and ends on 5/17/97, the calculation is as follows.
The day number of 1/3/97 is 3
The day number of 5/17/97 is 137
The length of the semester is 137-3+1=135
We add 1 to the difference of the days because we count the first day as part of the period.
The algorithm for calculating the day number for a date is complicated by leap years and by months of different lengths. We could code this algorithm as a void function named ComputeDay. The refund could then be computed by the following code segment.
ComputeDay(startMonth, startDay, startYear, start);
ComputeDay(lastMonth, lastDay, lastYear, last);
ComputeDay(withdrawMonth, withdrawDay, withdrawYear, withdraw);
fraction = float(last - withdraw + l) / float(last - start + l);
refund = tuition * fraction;
The first three parameters to ComputeDay are received by the function, and the last one is returned to the caller. Because ComputeDay returns only one value, we can write it as a value-returning function instead of a void function. Let's look at how the code segment would be written if we had a valuereturning function named Day that returned the day number of a date in a given year.
start = Day(startMonth, startDay, startYear);
last = Day(lastMonth, lastDay, lastYear);
withdraw = Day(withdrawMonth, withdrawDay, withdrawYear);
fraction = float(last - withdraw + 1) / float(last - start + 1);
refund = tuition * fraction;
The second version of the code segment is much more intuitive. Because Day is a value-returning function, you know immediately that all its parame-

 
< previous page page_405 next page >