Modula 2 Quick Syntax Reference
Beginners Version
Program Structure
MODULE ModuleName;
FROM IO IMPORT WrStr, WrLn; import
general
procedures
CONST PI =
3.1416; constante-declarations
VAR aNumber: CARDINAL;
variabele-declarations
BEGIN
...
statements
END ModuleName.
Single Types
- CARDINAL
- INTEGER
- CHAR: characterconstant: 'a' (note the difference
between the letter 'a' and the variable a)
- REAL
- conversion between cardinal <=> real:
- real := FLOAT(cardinal);
- cardinal := TRUNC(real); or use
VAL(CARDINAL, real)
- real constant format: -4.32E-3 (exponential
part
is optional)
- real := 4; is illegal, this should be :
real
:= 4.0;
- BOOLEAN: values TRUE & FALSE,
operators NOT, AND, OR
assignment: variable := value;
variables defined in the module (not in a procedure) are initialised
with a 0.
Structured Types
- ARRAY
- variable declaration: VAR array1: ARRAY [1..20] OF
CARDINAL;
- element of array: array1[2] := 1;
- special array: string (ARRAY OF CHARs)
- assignment: str := "bla";
- a 0 (not the letter '0') is placed in the array after the
last character to indicate the end of the string.
- multidimensional array :
- VAR matrix10by20: ARRAY [1..10],[1..20] OF CARDINAL;
- identical declaration: VAR matrix10by20:
ARRAY
[1..10], [1..20] OF CARDINAL;
- use: matrix10by20[5, 15] := 7; or
matrix10by20[5][15] := 7;
- RECORD
- declaration:
- VAR jan : RECORD
-
name,
firstName: ARRAY[1..35] OF CHAR;
-
length:
CARDINAL;
- END;
- use: jan.name := "Lemeire"; jan.firstName :=
"Jan"; jan.length := 190;
- record assignment is possible (rec1 := rec2;), but
record comparison not (rec1 = rec2)!
- for comparison: compare field by field: (rec1.a = rec2.a)
AND (rec1.b = rec2.b)
Procedures
PROCEDURE MyFunction(inputVariable1, inputVariable2: CARDINAL;
inputVariable3: WeekdayRc): BOOLEAN;
VAR result: BOOLEAN; variabele-declarations
BEGIN
...
statements
RETURN result;
END MyFunction;
- 2 types:
- with returnvariable: RETURN value;
- you may use multiple RETURN statements, but the procedure
stops immediately after a return
- without returnvariable
- you may use RETURN; to quit the procedure.
- resultVariable := MyFunction(cardinalVariable1,
cardinalVariable2, mondayRc);
- Local variables of a procedure are NOT initialised
automatically!! They can have any value at start.
- Scoop (variables, types & procedures): go out & up.
- a procedure must thus be defined before it can be used.
Operators
- NOT, AND, OR
- =,
# equals,
different
- +, -, *, /, MOD (modulo = rest), y
:=
ABS(x) (absolute value, for integers & reals)
- <, >, >=,
<= relational
operators
Use round brackets (haakjes) to indicate evaluation priorities
eg: IF x MOD 3 = 0 THEN gives an
error (the = operation is evaluated before the MOD)
this should be: IF (x MOD 3) = 0 THEN
Control Statements
IF (a > 5) OR (b > 6)
THEN condition
...
statements
ELSE
optional
...
statements
END;
WHILE NOT (i > 10)
DO condition
statements...
END;
REPEAT
statements...
UNTIL (i = 10);
FOR i := 10 TO 0 BY -1 DO By
clause
is optional
statements...
END;
Library RealMath (mathematical functions on reals)
- sqrt (x: REAL): REAL; Returns
the
positive square root of a positive x
- power (base, exponent: REAL): REAL; Returns
the value of the number base raised to the power exponent.
- Warning: base may not be negative!! Use ABS(base)
to make it positive.
Procedures (library IO) Take
these to when using black window
- WrLn;
skip to next line
- cardinalVariable := RdCard();
read cardinal
- WrCard(cardinalVariable, spaces);
write cardinal
- readVariable := RdInt();
- WrInt(intVariable, spaces);
- RdStr(stringVariable);
read string (variabele tussen de haakjes!!)
- WrStr(stringVariable);
write string to output
- charVariable :=
RdKey();
reads a key, use this instead of RdChar()
- charVariable :=
RdChar();
!! 2 ENTER-tokens stay in input buffer, are read in the next
time with a RdChar
- RdLn;
use this after RdChar to read the rest of the input and the
ENTER-tokens
- WrChar(charVariable);
- realVariable := RdReal();
read real from user input
- WrReal(realVariable, precision, spaces); write
real
to output (precision: number of digits)
- booleanVariable :=
RdBool(); ty
pe
"TRUE" and "FALSE"
- WrBool(booleanVariable, spaces);
Procedures (library Graph)
Colours are defined as numbers from 0 to 15, or use the constants
_clrBLACK, _clrBLUE, _clrGREEN, _clrCYAN, _clrRED, ...
Coordinates and colours are cardinals.
- BOOLEAN Init(1, 1 , width,
height); Creates
graphical
screen with, returns if OK
- Plot(x, y,
colour);
Draws a coloured point at x,y
- Line(x1, y1, x2, y2,
colour);
Draws a line from x1,y1 to x2,y2
- Rectangle(x1, y1, x2, y2, colour, Filled); Rectangle
between
x1,y1 and x2,y2, filled is a boolean
- Circle(x0, y0, radius,
colour);
Circle with center x0, y0 and radius
- Disc(x0, y0, radius,
colour);
Disc is a filled circle
- RawOutText(x0, y0, colour,
text);
Writes Text starting from x0,y0