4.6.3 Multiple return values

The method for decode-total-seconds returns three values: the hours, the minutes, and the seconds. To return the three values, the method uses the values function as the expression executed last in the body. The values function simply returns all its arguments as separate values. The ability to return multiple values allows a natural symmetry between encode-total-seconds and decode-total-seconds, as shown in Table 4.1.

Table 4.1 Symmetry of encode-total-seconds and decode-total-seconds.

Method

Parameter(s)

Return value(s)

encode-total-seconds

hours, minutes, seconds

total-seconds

decode-total-seconds

total-seconds

hours, minutes, seconds

Lines 4 and 5 of the decode-total-seconds method contain calls to truncate/. The truncate/ function is a built-in Dylan function. It takes two arguments, divides the first by the second, and returns two values: the result of the truncating division, and the remainder.

Comparison with C: In C, / on integers produces a truncated result. In Dylan, / on integers is implementation defined, and is not recommended for portable code. The Dylan functions named floor, ceiling, round, and truncate convert a rational or floating-point result to an integer with the appropriate rounding. The Dylan functions named floor/, ceiling/, round/, and truncate/ take two arguments. Those generic functions divide the first argument by the second argument, and return two values: the rounded or truncated result, and the remainder.