2. Declarations rules
IMPLEMENTATION MODULE MyModule;
CONST
TYPE
VAR
Constants declaration
Types declaration
Variables declaration
PROCEDURE MyProc;
CONST
TYPE
VAR
Constants declaration
Types declaration
Variables declaration
Every identifier occurring in a program must be introduced by a declaration, unless it is a standard identifier.
Declarations also specify certain permanent properties of an object, such as whether it is a constant, a type, a variable, a
procedure, or a module. The identifier is then used to refer to the associated object.
Julian Miglio 2019
BEGIN
END MyModule.
Procedures declaration
BEGIN
END MyProc;
Procedures declaration
3. Constants declaration
CONST
One = 1;
Pi = 3.14159;
PiOver2 = Pi / 2.0;
Debug = TRUE;
Integer constant
Floating point constant
Floating point constant
Boolean constant
Constants are objects that have a specific value assigned at compile time. Constants cannot change during the execution of the
program. For simple types, value is an expression giving the value of the constant. The expression must be made up of constants
only, so that the compiler can evaluate it at compile time. It is also possible declare constants of array and record types.
Julian Miglio 2019
Debug = TRUE;
Message = "That's not right!";
Boolean constant
String constant
4. Types declaration
TYPE
Integer = INTEGER;
SmallInt = [-10..10];
Color = (Red, Green, Blue);
CharPointer = POINTER TO CHAR;
ColorSet = SET OF COLOR;
String = ARRAY [0..255] OF CHAR;
Integer type
Pointer type
Set type
Array type
Subrange type
Enumeration type
A data type determines a set of values which variables of that type may assume, and it associates an identifier with the type.
In the case of structured types, it also defines the structure of variables of this type. There are three different structures, namely
arrays, records, and sets. Standard types are: INTEGER, CARDINAL, BOOLEAN, REAL, CHAR, POINTER.
There are other useful types exported by the module SYSTEM that specified the machine size such as CARD16, INT16, CARD32,
INT32, BYTE, WORD and others.
Julian Miglio 2019
Complex = RECORD A,B : REAL; END;
RealFunc = PROCEDURE(REAL) : REAL;
Record (structure) type
Procedure type
5. Variables declaration
VAR
I, J : INTEGER;
MAT : ARRAY [1..3],[1..3] OF REAL;
BackGround, ForeGround : Color;
PossibleColors : SET OF Color;
RecordPointer : POINTER TO RecType;
Integer variable
Set variable
Pointer to record variable
Array variable
Enumeration variable
Variable declaration is the definition and allocation of memory of a particular type. The data type determines the set of values
that a variable may assume and the operators that are applicable to it, it also defines the structure of the variable.
Each of the identifiers to the left of the colon is declared as a variable of the type that is specified to the right of the colon.
Julian Miglio 2019
6. Procedures and functions declaration
PROCEDURE Error;
BEGIN
WriteString('ERROR');
END Error;
PROCEDURE tan(x : REAL) : REAL;
BEGIN
RETURN sin(x) / cos(x);
END tan;
Function with an input parameter and a return
parameter
Procedure without parameters
Procedure declarations consist of a procedure heading and a block. The heading specifies the identifier and the formal
parameters, the block contains other declarations and executable statements. There are two kinds of procedures: procedures and
functions. The first is activated by a procedure call, while the second is called in an expression and returns a value.
Procedures are sequences of instructions that perform a specific task. Usually, procedures are used when the same sequence of
code is required more than once in a program. In this case, the procedure is written once and is called from several places.
Julian Miglio 2019
PROCEDURE tan(x : REAL) : REAL; FORWARD; Forward declaration
7. Reference
• Modula-2: Abstractions for Data and Programming Structures:
http://www.arjay.bc.ca/Modula-2/Text/index.html
• ISO/IEC 10514 Modula-2, Base Language: https://www.iso.org/standard/18583.html
• Modula2.org: https://www.modula2.org/
Julian Miglio 2019
• Modula2.org: https://www.modula2.org/