ZetCode

Modula-2 open arrays

last modified November 26, 2026 Modula-2

A parameter with a named array type accepts arrays of exactly that type. An open array parameter, written ARRAY OF T without bounds, accepts any one-dimensional array whose elements have type T, whatever its length and index range. Open arrays let one procedure work on arrays of every size, and on strings of every length.

An open array parameter

Inside the procedure, an open array is indexed from 0, and the standard function HIGH returns the index of its last element. A loop from 0 to HIGH(a) visits every element.

total.mod
MODULE total;

FROM InOut IMPORT WriteString, WriteLn, WriteInt;

VAR
  week: ARRAY [0..6] OF INTEGER;
  pair: ARRAY [0..1] OF INTEGER;

PROCEDURE Sum(a: ARRAY OF INTEGER): INTEGER;
VAR
  i: CARDINAL;
  s: INTEGER;
BEGIN
  s := 0;
  FOR i := 0 TO HIGH(a) DO
    s := s + a[i]
  END;
  RETURN s
END Sum;

BEGIN
  week[0] := 4;  week[1] := 7;  week[2] := 3;  week[3] := -2;
  week[4] := 0;  week[5] := 5;  week[6] := 9;
  pair[0] := 100;
  pair[1] := -40;
  WriteString("week: ");
  WriteInt(Sum(week), 0);
  WriteLn;
  WriteString("pair: ");
  WriteInt(Sum(pair), 0);
  WriteLn
END total.

The same Sum adds the seven elements of week and the two elements of pair. HIGH(a) is 6 in the first call and 1 in the second. The procedure knows nothing about the declared types of the arguments.

$ gm2 total.mod -o total
$ ./total
week: 26
pair: 60

Indexes start at zero

The index range of the argument does not matter. Whatever the bounds of the caller's array, the open array parameter numbers its elements from 0 to the number of elements minus 1.

years.mod
MODULE years;

FROM InOut IMPORT WriteString, WriteLn, WriteCard;

VAR
  rain: ARRAY [2020..2025] OF CARDINAL;

PROCEDURE Wettest(a: ARRAY OF CARDINAL): CARDINAL;
VAR
  i, best: CARDINAL;
BEGIN
  best := 0;
  FOR i := 1 TO HIGH(a) DO
    IF a[i] > a[best] THEN
      best := i
    END
  END;
  RETURN best
END Wettest;

PROCEDURE Show(a: ARRAY OF CARDINAL);
VAR
  i: CARDINAL;
BEGIN
  WriteString("HIGH = ");
  WriteCard(HIGH(a), 0);
  WriteString(":");
  FOR i := 0 TO HIGH(a) DO
    WriteCard(a[i], 5)
  END;
  WriteLn
END Show;

BEGIN
  rain[2020] := 612;  rain[2021] := 705;  rain[2022] := 548;
  rain[2023] := 811;  rain[2024] := 690;  rain[2025] := 734;
  Show(rain);
  WriteString("Wettest year: ");
  WriteCard(2020 + Wettest(rain), 0);
  WriteLn
END years.

rain is indexed by the years 2020 to 2025, but inside Show and Wettest the same six elements have the indexes 0 to 5, and HIGH is 5. Wettest returns a position, and the caller adds 2020 to turn it back into a year. An open array always has at least one element, so a[0] is a safe start.

$ gm2 years.mod -o years
$ ./years
HIGH = 5:  612  705  548  811  690  734
Wettest year: 2023

VAR open arrays

An open array parameter can be a VAR parameter. The procedure then changes the caller's array, of whatever length.

reverse.mod
MODULE reverse;

FROM InOut IMPORT WriteString, WriteLn, WriteInt;

VAR
  small: ARRAY [1..4] OF INTEGER;
  large: ARRAY [0..9] OF INTEGER;

PROCEDURE Fill(VAR a: ARRAY OF INTEGER; first, step: INTEGER);
VAR
  i: CARDINAL;
BEGIN
  FOR i := 0 TO HIGH(a) DO
    a[i] := first;
    first := first + step
  END
END Fill;

PROCEDURE Reverse(VAR a: ARRAY OF INTEGER);
VAR
  i, j: CARDINAL;
  t: INTEGER;
BEGIN
  i := 0;
  j := HIGH(a);
  WHILE i < j DO
    t := a[i];
    a[i] := a[j];
    a[j] := t;
    INC(i);
    DEC(j)
  END
END Reverse;

PROCEDURE Show(a: ARRAY OF INTEGER);
VAR
  i: CARDINAL;
BEGIN
  FOR i := 0 TO HIGH(a) DO
    WriteInt(a[i], 4)
  END;
  WriteLn
END Show;

BEGIN
  Fill(small, 1, 1);
  Fill(large, 50, -10);
  Reverse(small);
  Reverse(large);
  Show(small);
  Show(large)
END reverse.

Fill stores an arithmetic sequence and Reverse swaps elements from both ends until the indexes meet. Both work on the four-element and the ten-element array. Show only reads its array, so it takes a value parameter; a value open array is a copy, like any value parameter.

$ gm2 reverse.mod -o reverse
$ ./reverse
   4   3   2   1
 -40 -30 -20 -10   0  10  20  30  40  50

Strings of any length

ARRAY OF CHAR accepts a string constant of any length as well as a character array. A string may fill the whole array, or end earlier with the character 0C, so a length function checks both.

banner.mod
MODULE banner;

FROM InOut IMPORT WriteString, WriteLn, Write;

VAR
  name: ARRAY [0..19] OF CHAR;

PROCEDURE Length(s: ARRAY OF CHAR): CARDINAL;
VAR
  n: CARDINAL;
BEGIN
  n := 0;
  WHILE (n <= HIGH(s)) AND (s[n] # 0C) DO
    INC(n)
  END;
  RETURN n
END Length;

PROCEDURE Repeat(ch: CHAR; count: CARDINAL);
VAR
  i: CARDINAL;
BEGIN
  FOR i := 1 TO count DO
    Write(ch)
  END
END Repeat;

PROCEDURE Banner(text: ARRAY OF CHAR);
BEGIN
  Repeat('*', Length(text) + 4);
  WriteLn;
  WriteString("* ");
  WriteString(text);
  WriteString(" *");
  WriteLn;
  Repeat('*', Length(text) + 4);
  WriteLn
END Banner;

BEGIN
  Banner("Hello there!");
  Banner("Modula-2");
  name := "Niklaus Wirth";
  Banner(name)
END banner.

Length stops at the first 0C or at the end of the array, whichever comes first; WriteString from InOut is declared with the same kind of parameter. The variable name has room for 20 characters, but its length is 13. Do not take HIGH of a string parameter as its length: GNU Modula-2 passes a string constant with a terminating 0C, so HIGH("Modula-2") inside the procedure is 8, not 7.

$ gm2 banner.mod -o banner
$ ./banner
****************
* Hello there! *
****************
************
* Modula-2 *
************
*****************
* Niklaus Wirth *
*****************

Passing rows of a table

A row of a two-dimensional array is a one-dimensional array, so it can be passed to an open array parameter. The procedure works on rows of any table with the right element type.

scores.mod
MODULE scores;

FROM InOut IMPORT WriteString, WriteLn, WriteCard;

CONST
  Players = 3;
  Rounds = 4;

VAR
  points: ARRAY [1..Players] OF ARRAY [1..Rounds] OF CARDINAL;
  p: CARDINAL;

PROCEDURE Best(a: ARRAY OF CARDINAL): CARDINAL;
VAR
  i, max: CARDINAL;
BEGIN
  max := a[0];
  FOR i := 1 TO HIGH(a) DO
    IF a[i] > max THEN
      max := a[i]
    END
  END;
  RETURN max
END Best;

PROCEDURE SetRow(VAR row: ARRAY OF CARDINAL; a, b, c, d: CARDINAL);
BEGIN
  row[0] := a;
  row[1] := b;
  row[2] := c;
  row[3] := d
END SetRow;

BEGIN
  SetRow(points[1], 12, 30, 7, 18);
  SetRow(points[2], 25, 9, 14, 21);
  SetRow(points[3], 8, 16, 33, 10);
  FOR p := 1 TO Players DO
    WriteString("Player ");
    WriteCard(p, 0);
    WriteString(" best round:");
    WriteCard(Best(points[p]), 3);
    WriteLn
  END
END scores.

points[p] is the row of player p. SetRow fills a row through a VAR open array, and Best finds the largest element of a row. Inside both procedures the four rounds have the indexes 0 to 3.

$ gm2 scores.mod -o scores
$ ./scores
Player 1 best round: 30
Player 2 best round: 25
Player 3 best round: 33

No whole-array assignment

The size of an open array is known only at run time, so an open array cannot be assigned as a whole, neither to nor from another array.

assign.mod
MODULE assign;

TYPE
  Row = ARRAY [0..3] OF INTEGER;

VAR
  saved: Row;

PROCEDURE Save(a: ARRAY OF INTEGER);
BEGIN
  saved := a
END Save;

BEGIN
  Save(saved)
END assign.

saved := a would be legal if a had the type Row. With an open array parameter the compiler rejects it.

$ gm2 assign.mod -o assign
assign.mod:11:12: error: In procedure ‘Save’: in assignment, cannot assign from an unbounded array ‘a’
   11 |   saved := a
      |            ^

Copying element by element

A copy between open arrays is a loop. When the arrays differ in length, the loop stops at the end of the shorter one.

copy.mod
MODULE copy;

FROM InOut IMPORT WriteString, WriteLn, WriteInt, WriteCard;

VAR
  three: ARRAY [1..3] OF INTEGER;
  five: ARRAY [1..5] OF INTEGER;

PROCEDURE Copy(src: ARRAY OF INTEGER; VAR dst: ARRAY OF INTEGER): CARDINAL;
VAR
  i, n: CARDINAL;
BEGIN
  n := HIGH(src);
  IF HIGH(dst) < n THEN
    n := HIGH(dst)
  END;
  FOR i := 0 TO n DO
    dst[i] := src[i]
  END;
  RETURN n + 1
END Copy;

PROCEDURE Show(a: ARRAY OF INTEGER);
VAR
  i: CARDINAL;
BEGIN
  FOR i := 0 TO HIGH(a) DO
    WriteInt(a[i], 3)
  END;
  WriteLn
END Show;

BEGIN
  three[1] := 7; three[2] := 8; three[3] := 9;
  five[1] := 1; five[2] := 2; five[3] := 3; five[4] := 4; five[5] := 5;
  WriteString("copied ");
  WriteCard(Copy(three, five), 0);
  WriteString(":");
  Show(five);
  five[1] := 0;
  WriteString("copied ");
  WriteCard(Copy(five, three), 0);
  WriteString(":");
  Show(three)
END copy.

Copy takes the smaller of the two HIGH values as its last index and returns the number of elements it copied. Copying three elements into the five-element array leaves the last two as they were; copying the five-element array into the three-element one keeps only its first three elements.

$ gm2 copy.mod -o copy
$ ./copy
copied 3:  7  8  9  4  5
copied 3:  0  8  9

A bar chart

The last program prints a bar chart. The labels are an array of character arrays, and one procedure receives both the labels and the values as open arrays.

chart.mod
MODULE chart;

FROM InOut IMPORT WriteString, WriteLn, WriteCard, Write;

CONST
  Width = 30;

TYPE
  Label = ARRAY [0..11] OF CHAR;

VAR
  names: ARRAY [0..4] OF Label;
  sales: ARRAY [0..4] OF CARDINAL;

PROCEDURE Length(s: ARRAY OF CHAR): CARDINAL;
VAR
  n: CARDINAL;
BEGIN
  n := 0;
  WHILE (n <= HIGH(s)) AND (s[n] # 0C) DO
    INC(n)
  END;
  RETURN n
END Length;

PROCEDURE WriteLeft(s: ARRAY OF CHAR; width: CARDINAL);
VAR
  i: CARDINAL;
BEGIN
  WriteString(s);
  FOR i := Length(s) + 1 TO width DO
    Write(' ')
  END
END WriteLeft;

PROCEDURE Chart(labels: ARRAY OF Label; values: ARRAY OF CARDINAL);
VAR
  i, j, max: CARDINAL;
BEGIN
  max := 1;
  FOR i := 0 TO HIGH(values) DO
    IF values[i] > max THEN
      max := values[i]
    END
  END;
  FOR i := 0 TO HIGH(values) DO
    WriteLeft(labels[i], 10);
    WriteCard(values[i], 4);
    Write(' ');
    FOR j := 1 TO values[i] * Width DIV max DO
      Write('#')
    END;
    WriteLn
  END
END Chart;

BEGIN
  names[0] := "Apples";   sales[0] := 420;
  names[1] := "Pears";    sales[1] := 135;
  names[2] := "Plums";    sales[2] := 288;
  names[3] := "Cherries"; sales[3] := 510;
  names[4] := "Quinces";  sales[4] := 47;
  Chart(names, sales)
END chart.

Chart takes ARRAY OF Label, an open array whose elements are fixed character arrays, and passes each labels[i] on to the open ARRAY OF CHAR parameter of WriteLeft. The longest bar is Width characters, and the others are scaled to it with whole-number arithmetic.

$ gm2 chart.mod -o chart
$ ./chart
Apples     420 ########################
Pears      135 #######
Plums      288 ################
Cherries   510 ##############################
Quinces     47 ##

Next steps

Procedures can be passed to other procedures as well as arrays. The procedure types article stores procedures in variables and passes them as parameters.

Source

The GNU Modula-2 manual

Modula-2 language reference at modula2.org

In this article we have written Modula-2 procedures with open array parameters, used HIGH and zero-based indexes, changed arrays through VAR open arrays, passed strings and table rows, and copied between arrays of different lengths. The arrays article covers the fixed arrays passed to these procedures, and the procedures article value and VAR parameters.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Modula-2 tutorials.