All of the examples, so far, have only required zero or one output from a procedure. Many problems involve more than one output. In Modula 3 there are two ways to achieve this:
Here is an example that illustrates the difference:
Write a procedure to determine the century and the two-digit year within
the century from a normal year form.
One input: the year. Two outputs: century and two-digit year. Here is one possible solution:
PROCEDURE BreakdownYear(year : INTEGER;
VAR century, twoDigitYear : INTEGER) =
BEGIN
twoDigitYear := year MOD 100;
(* Be strict about centuries starting in 01. *)
IF twoDigitYear = 0 THEN
century := (year DIV 100);
ELSE
century := (year DIV 100) + 1;
END;
END BreakdownYear;
Here is the alternative, which looks much more like a function by using a RECORD to combine the two output values:
TYPE
Components =
RECORD
century : INTEGER;
twoDigitYear : INTEGER;
END;
PROCEDURE BreakdownYear(year : INTEGER) : Components =
VAR
components : Components;
BEGIN
components.twoDigitYear := year MOD 100;
(* Be strict about centuries starting in 01. *)
IF components.twoDigitYear = 0 THEN
components.century := (year DIV 100);
ELSE
components.century := (year DIV 100) + 1;
END;
RETURN components;
END BreakdownYear;
Whilst this is not a style that you are used to, it is better Software Engineering style to write non-side-effecting procedures and avoid using VAR-parameters wherever possible.
To Previous PageTo Overview
To Next Page