[Santa Clara University]
Department of Mathematics
and Computer Science

Math 169

Modula-2/Pascal Comparison


OVERVIEW and COMPARISONS
between PASCAL and MODULA-2

A Modula-2 program is a collection of separately compilable sections called MODULEs. The main program is itself a MODULE. Each MODULE begins with the header line MODULE name and ends with the reserved word END, followed by the name followed by a period.

Each MODULE can have its own PROCEDUREs and there is no need to have more than one MODULE. The reserved word PROGRAM does not exist.

Multiple MODULEs can be important in large software-engineering type projects where separate groups/individuals are working on separate segments of a program code. Each MODULE can be protected against unauthorized changes in its data structures through encapsulation. In addition, good code can be compiled once and not have to be re-compiled every time a small change is being made in another section.

MODULEs can be written in two parts: a DEFINITION MODULE and an IMPLEMENTATION MODULE. This enables a program to encapsulate data and prevent a user from directly (and incorrectly) accessing the underlying data structure. The DEFINITION MODULE contains procedure headings only, while the IMPLEMENTATION MODULE contains the complete code.

Key words MUST be all UPPER CASE. Procedure (and MODULE) names are usually Capitalized and must be consistent in the use of UPPER and lower case letters. In general, identifiers are "case-sensitive," i.e., N and n are DIFFERENT identifiers, as are FreeList and freelist.

To "borrow" procedures from other MODULEs, an IMPORT clause is used between the MODULE header line and the identifier declarations. To "loan" procedures out to other MODULEs, an EXPORT clause is (sometimes) used.

BEGINs are used less frequently than in Pascal, normally only at the beginning of a PROCEDURE or MODULE. The corresponding END at the END of a PROCEDURE or MODULE must be followed with the name of the PROCEDURE or MODULE, e.g. END MainProgram.

Modula-2 does NOT have a GOTO statement or a FUNCTION reserved word.

Comments can only be enclosed by (* *) markers, but CAN be nested.

The pound sign (#) can be used in lieu of <> in boolean relational expressions.

I-O procedures in Modula-2 must be imported from the appropriate i-o MODULE (e.g., "stdIO" for Univ. Ill. freeware compiler, "InOut" and "RealInOut" for other compilers), and are distinguished by TYPE. The following are standard: WriteString, WriteInt, WriteReal, WriteLn, ReadString, ReadInt, ReadReal.

In the CONST definition, arithmetic expressions ARE allowed, e.g. CONST N = 4; MaxLength = 2*N;

There is a second INTEGER type, called CARDINAL, which is an unsigned integer. In Modula-2, CARDINALs are the rule, and INTEGERs are the exception. INTEGERs should be used only when a variable is have negative values.

String constants can be delimited by single or double quotes.

Sets are delimited by curly braces (instead of square brackets as in Pascal).

When defining a pointer type, one uses the reserved words POINTER TO instead of the preceding up-arrow as in Pascal. For example, TYPE PTR = POINTER TO Node;

All structured statements end with an explicit ending reserved word: for the REPEAT, it is UNTIL; for all others, it is END. There is no BEGIN. For example:

	IF i > 0 THEN
		WriteString('Truth');
		i := -1;
	END;
	WHILE j>3 DO
		ReadInt(j,k);
	END;
Thus, the IF, WHILE, FOR statements MUST end with an END, even if the action is ONLY ONE STATEMENT.

The CASE statement may have an ELSE clause. Statement sequences for each case are separated by a vertical bar (|) (i.e. no BEGIN and END if an option contains more than one statement). A bar CANNOT appear before the ELSE clause.

In the IF statement, a nested IF can be introduced with the reserved word ELSIF. No BEGIN..END pairs are necessary between the THEN and the ELSE or ELSIF and the statement concludes with an END.

The LOOP statement is a generalized repetition structure which allows mid-loop existing via an EXIT statement. It ends with an END statement, and somewhere inside the loop, there should be an (one or more) EXIT statement(s) (frequently the action of an IF statement). For example,

	LOOP
		ReadInt(i);
		IF i = 0 THEN EXIT
		ELSE
			Process(i);
		END;
	END;
There is NO specific FUNCTION in Modula-2. However, PROCEDUREs can be used as functions if they are given a TYPE, and the RETURN statement is used. The PROCEDURE header is given as type as the last element (similar to the Pascal FUNCTION header). E.g.,
	PROCEDURE Signum (Freud: INTEGER): INTEGER;
Instead of assigning the procedure name a value, one returns a value via the RETURN statement, e.g.,
	IF Freud < 0 THEN
		RETURN -1
	ELSE
		RETURN 0
	END
The value following a RETURN may be an expression. A RETURN can also be used in a non-functional PROCEDURE optionally for an early return.

Also included as part of the standard library functions are functions INC(x) and DEC(x) which increment and decrement x by 1, similar to the C language constructs x++ and x-- or the LISP functions ADD1 and SUB1.

The "standard" procedures and functions of the languages can be IMPORTed from the following modules:

FROM InOut (*or stdIO*) IMPORT WriteString, WriteInt, WriteLn, ReadInt, ReadString, ReadStrings, Read (for single characters).

FROM Storage IMPORT ALLOCATE, DEALLOCATE (NOTE: ALLOCATE must be IMPORTed whenever NEW is used, and DEALLOCATE whenever DISPOSE is used.)


My email address is dsmolarski@math.scu.edu

This page last updated 25 March 2000.