ZetCode

Modula-2 program structure

last modified November 26, 2026 Modula-2

A Modula-2 program is made of modules. This article takes the program module apart: its heading, the import list, the declarations, and the body. It then shows the rules for upper and lower case, and ends with the two other kinds of module, the definition module and the implementation module.

Kinds of modules

Modula-2 has three kinds of compilation units. A program module is the main program; its body runs when the program starts. A definition module lists the constants, types, variables and procedures that a library module exports. An implementation module contains the code behind a definition module. The examples in the first part of this article are program modules.

The smallest module

A program module needs only a heading and an end. The heading is MODULE, a name, and a semicolon; the end is END, the same name, and a period.

empty.mod
MODULE empty;
END empty.

The module has no imports, no declarations and no statements, so the program does nothing and exits with status 0. The period after the name marks the end of the compilation unit, and GNU Modula-2 rejects any text that follows it.

$ gm2 empty.mod -o empty
$ ./empty
$ echo $?
0

The import list

Imports come directly after the heading, before any declaration. A program may have several import lines, each naming one module with FROM … IMPORT or importing whole modules with IMPORT.

imports.mod
MODULE imports;

FROM InOut IMPORT WriteString, WriteLn;
FROM RealInOut IMPORT WriteReal;
IMPORT MathLib0;

BEGIN
  WriteString("Square root of 2:");
  WriteReal(MathLib0.sqrt(2.0), 12);
  WriteLn
END imports.

InOut and RealInOut supply output procedures, and MathLib0 is the small mathematical library of Wirth's book. The sqrt procedure is written with its module name because the whole module was imported. Putting an import after a VAR section is a syntax error.

$ gm2 imports.mod -o imports
$ ./imports
Square root of 2:    1.414214

Declarations

Between the imports and the body come the declarations. A CONST section declares constants, TYPE declares new type names, VAR declares variables, and PROCEDURE declares a procedure. The sections may appear in any order and any section may appear more than once.

declarations.mod
MODULE declarations;

FROM InOut IMPORT WriteString, WriteLn, WriteInt;

CONST
  Limit = 10;

TYPE
  Score = INTEGER;

VAR
  count: INTEGER;

VAR
  best: Score;

PROCEDURE Show;
BEGIN
  WriteString("count = ");
  WriteInt(count, 0);
  WriteLn;
  WriteString("best = ");
  WriteInt(best, 0);
  WriteLn
END Show;

BEGIN
  count := Limit;
  best := Limit * 3;
  Show
END declarations.

Score is a new name for INTEGER, so best holds integers. The procedure Show has its own BEGIN and END Show and is called from the body by its name. Procedures are covered in detail in the procedures article.

$ gm2 declarations.mod -o declarations
$ ./declarations
count = 10
best = 30

Order of declarations

Some Modula-2 compilers read the source only once and require every name to be declared before it is used. GNU Modula-2 reads the whole module before it checks names, so a constant may refer to a constant declared below it, and a procedure may call a procedure declared after it.

order.mod
MODULE order;

FROM InOut IMPORT WriteString, WriteLn, WriteInt;

CONST
  Total = Part * 2;
  Part = 21;

PROCEDURE First;
BEGIN
  WriteString("first, then ");
  Second
END First;

PROCEDURE Second;
BEGIN
  WriteString("second");
  WriteLn
END Second;

BEGIN
  First;
  WriteString("Total: ");
  WriteInt(Total, 0);
  WriteLn
END order.

Total uses Part before its declaration, and First calls Second. Code that has to compile with one-pass compilers declares everything first; most programs follow that style anyway because it reads better.

$ gm2 order.mod -o order
$ ./order
first, then second
Total: 42

The module body

The body is the statement sequence between BEGIN and the final END. Statements are separated by semicolons, not terminated by them. An extra semicolon before END creates an empty statement, which is allowed and does nothing.

body.mod
MODULE body;

FROM InOut IMPORT WriteString, WriteLn, WriteInt;

VAR
  width, height: INTEGER;

BEGIN
  width := 7;
  height := 4;
  WriteString("Area: "); WriteInt(width * height, 0); WriteLn;
  WriteString("Perimeter: "); WriteInt(2 * (width + height), 0); WriteLn;
END body.

Several statements may share a line, and the semicolon after the last WriteLn adds an empty statement before END. Both forms are common in Modula-2 code. Line breaks and indentation have no meaning to the compiler.

$ gm2 body.mod -o body
$ ./body
Area: 28
Perimeter: 22

Upper and lower case

Modula-2 distinguishes upper and lower case in every name. Keywords such as BEGIN and standard identifiers such as INTEGER are written in upper case. Two names that differ only in case are two different names.

letters.mod
MODULE letters;

FROM InOut IMPORT WriteString, WriteLn, WriteInt;

VAR
  count, Count, COUNT: INTEGER;

BEGIN
  count := 1;
  Count := 2;
  COUNT := 3;
  WriteString("count = "); WriteInt(count, 0); WriteLn;
  WriteString("Count = "); WriteInt(Count, 0); WriteLn;
  WriteString("COUNT = "); WriteInt(COUNT, 0); WriteLn
END letters.

The three variables are independent. Real programs avoid names like these, but the example shows that case matters. A type written as integer is an unknown name, and a keyword written in lower case is an ordinary identifier, which usually leads to a syntax error.

$ gm2 letters.mod -o letters
$ ./letters
count = 1
Count = 2
COUNT = 3

Case errors

A misspelled case is one of the most common mistakes for programmers coming from Pascal, where case does not matter. The next program writes the type in lower case.

lowercase.mod
MODULE lowercase;

FROM InOut IMPORT WriteInt, WriteLn;

VAR
  count: integer;

BEGIN
  count := 3;
  WriteInt(count, 0);
  WriteLn
END lowercase.

The compiler does not know the name integer and lists it among the unresolved symbols of the module.

$ gm2 lowercase.mod -o lowercase
lowercase.mod:6:10: error: In program module ‘lowercase’: unknown symbol ‘integer’
    6 |   count: integer;
      |          ^~~~~~~
lowercase.mod:12:14: error: the following unknown symbols in module ‘lowercase’ were unresolved: integer
   12 | END lowercase.
      |              ^

Definition and implementation modules

A library module has two parts. The definition module, in a .def file, declares what the module exports. The implementation module, in a .mod file of the same name, contains the code.

Greeting.def
DEFINITION MODULE Greeting;

PROCEDURE Greet(name: ARRAY OF CHAR);

END Greeting.

The definition module declares only the heading of Greet. The parameter type ARRAY OF CHAR accepts a string of any length; it is explained in the open arrays article.

Greeting.mod
IMPLEMENTATION MODULE Greeting;

FROM InOut IMPORT WriteString, WriteLn;

PROCEDURE Greet(name: ARRAY OF CHAR);
BEGIN
  WriteString("Hello there, ");
  WriteString(name);
  WriteLn
END Greet;

BEGIN
  WriteString("Greeting is ready");
  WriteLn
END Greeting.

The implementation module repeats the procedure heading and adds its body. An implementation module may have a body of its own, which runs once, before the body of the program module that imports it. It is used to initialise the module's variables.

welcome.mod
MODULE welcome;

FROM Greeting IMPORT Greet;

BEGIN
  Greet("Niklaus")
END welcome.

The program imports Greet as it imports procedures from InOut. The library module is compiled on its own with -c, which produces an object file, and the program is linked with it. The output shows that the body of Greeting runs first.

$ gm2 -c Greeting.mod
$ gm2 welcome.mod Greeting.o -o welcome
$ ./welcome
Greeting is ready
Hello there, Niklaus

Next steps

The data types article describes the basic types used in declarations, and the constants and literals article shows how values are written in the source. Library modules are covered in depth in the modules article.

Source

The GNU Modula-2 manual

Modula-2 language reference at modula2.org

In this article we have looked at the parts of a program module: the heading, the import list, the declarations and the body. We have seen that names are case-sensitive and compiled a small library module with a definition and an implementation part. The basics article covers the statements used inside the body.

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.