Grouping Data

Multiple Outputs

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:


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 Page

To Overview

To Next Page