ZetCode

Modula-2 basics

last modified November 26, 2026 Modula-2

This article covers the parts of Modula-2 that almost every program uses: variables, assignment, constants, simple arithmetic, output with the InOut and RealInOut modules, the two forms of import, and comments. Conditionals, loops and procedures have their own articles.

Variables

Variables are declared in a VAR section before the body of the module. Each declaration gives one or more names, a colon, and a type. A value is stored in a variable with the assignment operator :=.

variables.mod
MODULE variables;

FROM InOut IMPORT WriteString, WriteLn, WriteInt;

VAR
  apples: INTEGER;

BEGIN
  apples := 12;
  WriteString("Apples: ");
  WriteInt(apples, 0);
  WriteLn
END variables.

The variable apples has the type INTEGER. A declared variable has no defined value until something is assigned to it, so the body assigns 12 before printing it. The single = sign is not assignment; it compares two values.

$ gm2 variables.mod -o variables
$ ./variables
Apples: 12

Assignment and arithmetic

Several variables of the same type can share one declaration. An expression on the right side of := is computed and its value is stored in the variable on the left. For integers, DIV is the integer division and MOD the remainder.

arithmetic.mod
MODULE arithmetic;

FROM InOut IMPORT WriteString, WriteLn, WriteInt;

VAR
  apples, people, each, left: INTEGER;

BEGIN
  apples := 17;
  people := 5;
  each := apples DIV people;
  left := apples MOD people;

  WriteString("Each person gets ");
  WriteInt(each, 0);
  WriteString(" apples, ");
  WriteInt(left, 0);
  WriteString(" are left over.");
  WriteLn;

  apples := apples - left;
  WriteString("Apples shared: ");
  WriteInt(apples, 0);
  WriteLn
END arithmetic.

The statement apples := apples - left reads the old value of apples, subtracts, and stores the result back. In Wirth's definition the / operator divides real numbers; GNU Modula-2 also accepts it on two integers and truncates the result, but DIV is the portable form. The operators article covers all the operators and their precedence.

$ gm2 arithmetic.mod -o arithmetic
$ ./arithmetic
Each person gets 3 apples, 2 are left over.
Apples shared: 15

Constants

A constant is declared in a CONST section with = instead of a type. Its value is fixed when the program is compiled and cannot be assigned to. The type of the constant comes from its value.

constants.mod
MODULE constants;

FROM InOut IMPORT WriteString, WriteLn, WriteInt;

CONST
  SeatsPerRow = 24;
  Rows = 18;
  Seats = SeatsPerRow * Rows;
  Title = "Seats in the hall: ";

VAR
  sold: INTEGER;

BEGIN
  sold := 317;
  WriteString(Title);
  WriteInt(Seats, 0);
  WriteLn;
  WriteString("Free seats: ");
  WriteInt(Seats - sold, 0);
  WriteLn
END constants.

The constant Seats is computed from two other constants, and Title is a string constant. Constants are often written with an initial capital letter to tell them apart from variables, but the language does not require it. The constants and literals article covers the forms that constant values can take.

$ gm2 constants.mod -o constants
$ ./constants
Seats in the hall: 432
Free seats: 115

Real numbers

The REAL type holds floating-point numbers. Real literals must contain a decimal point, so 2.0 is a real and 2 is an integer. Reals are printed with WriteReal from the RealInOut module, which takes the value and a field width.

reals.mod
MODULE reals;

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

CONST
  Pi = 3.14159265;

VAR
  radius, area, circumference: REAL;

BEGIN
  radius := 2.5;
  area := Pi * radius * radius;
  circumference := 2.0 * Pi * radius;

  WriteString("Radius:        ");
  WriteReal(radius, 10);
  WriteLn;
  WriteString("Area:          ");
  WriteReal(area, 10);
  WriteLn;
  WriteString("Circumference: ");
  WriteReal(circumference, 10);
  WriteLn;
  WriteString("Ratio:         ");
  WriteReal(circumference / area, 10);
  WriteLn
END reals.

WriteReal prints six decimal places and pads the number on the left to the field width, so the numbers line up in a column. The / operator divides two reals. Writing 2 * Pi would not compile, because Modula-2 does not mix integers and reals in one expression.

$ gm2 reals.mod -o reals
$ ./reals
Radius:          2.500000
Area:           19.634954
Circumference:  15.707963
Ratio:           0.800000

Characters

A CHAR variable holds a single character. A character literal is a one-character string in single or double quotes. InOut prints a character with Write.

chars.mod
MODULE chars;

FROM InOut IMPORT WriteString, WriteLn, Write;

VAR
  first, last: CHAR;

BEGIN
  first := 'N';
  last := "W";
  WriteString("Initials: ");
  Write(first);
  Write('.');
  Write(last);
  Write('.');
  WriteLn
END chars.

Both quote characters are allowed and mean the same thing. A string in double quotes may contain a single quote, and a string in single quotes may contain a double quote.

$ gm2 chars.mod -o chars
$ ./chars
Initials: N.W.

Qualified import

FROM InOut IMPORT WriteString makes the name WriteString visible on its own. The other form, IMPORT InOut, imports the module itself, and every procedure is then written with the module name in front of it.

qualified.mod
MODULE qualified;

IMPORT InOut, RealInOut;

VAR
  price: REAL;
  count: CARDINAL;

BEGIN
  price := 4.25;
  count := 3;
  InOut.WriteString("Items: ");
  InOut.WriteCard(count, 0);
  InOut.WriteLn;
  InOut.WriteString("Price: ");
  RealInOut.WriteReal(price, 0);
  InOut.WriteLn
END qualified.

A qualified name such as InOut.WriteString shows where each procedure comes from, at the cost of longer lines. It also avoids clashes when two modules export procedures with the same name. The variable count is a CARDINAL, a whole number that cannot be negative, so it is printed with WriteCard.

$ gm2 qualified.mod -o qualified
$ ./qualified
Items: 3
Price: 4.250000

Comments

A comment starts with (* and ends with *). It may span several lines and may appear anywhere a space is allowed. Unlike comments in C or Pascal, Modula-2 comments nest.

comments.mod
(* Prints the number of seconds in a day.
   Written for the Modula-2 basics article. *)
MODULE comments;

FROM InOut IMPORT WriteString, WriteLn, WriteCard;

CONST
  SecondsPerMinute = 60;
  MinutesPerHour = 60;
  HoursPerDay = 24;

BEGIN
  WriteString("Seconds per day: ");
  WriteCard(HoursPerDay * MinutesPerHour * SecondsPerMinute, 0);
  WriteLn
  (* WriteString("Seconds per week: ");  (* not needed yet *)
     WriteCard(7 * HoursPerDay * MinutesPerHour * SecondsPerMinute, 0);
     WriteLn *)
END comments.

The last comment disables three lines of code, and one of those lines has a comment of its own. Because comments nest, the inner *) closes only the inner comment, and the outer comment ends at the last *). This makes it safe to comment out a block of code that already contains comments.

$ gm2 comments.mod -o comments
$ ./comments
Seconds per day: 86400

Next steps

The program structure article describes the parts of a module and the order of its declarations. The data types article covers INTEGER, CARDINAL, REAL and the other basic types in detail.

Source

The GNU Modula-2 manual

Modula-2 language reference at modula2.org

In this article we have declared variables and constants, assigned values, computed with integers and reals, printed the results with InOut and RealInOut, used both forms of import, and written nested comments. The introduction covers the history of the language and the installation of GNU Modula-2.

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.