Download presentation
Presentation is loading. Please wait.
1
Introduction to Modula-2
Johan Parent 20/11/2018 Practicum I
2
Introduction 3 hours of total immersion into the programming language Modula-2 We start from the very basics Johan Parent 4K216 20/11/2018 Practicum I
3
Modula 2 : why? Why not Java, C, C++, C# ?! Learning
Looks like English Very structured Safe (easy?) Has all the modern features All programming languages are equal!! 20/11/2018 Practicum I
4
Programming Way to control a computer Compiler is clever
Write down the program (editor) Translate using special program (compiler) Execution by the computer Compiler is clever Translates in machine language Reports mistakes 20/11/2018 Practicum I
5
Modula 2 Has keywords: words from the language
Always in CAPITAL letters! DO, FOR, IF, WHILE, END, THEN, UNTIL, LOOP, ELSE, RECORD, ARRAY, OF, TYPE, CONST, IMPORT, FROM, SET, VAR, WITH, CASE, MODULE, REAL, INTEGER… 20/11/2018 Practicum I
6
Program structure Program is called a MODULE MODULE name; BEGIN
FROM …. IMPORT …; CONST pi = ; TYPE euro = REAL; BEGIN …. (* comment *) END name. 20/11/2018 Practicum I
7
Syntax Keywords completely in CAPITAL letters Spaces do not matter
White lines do not matter Signal the end of a command with semi colon ; End of program with a period . 20/11/2018 Practicum I
8
Variables Convenient way to:
Store values Manipulate values Closely related to the variable concept used in mathematics! 20/11/2018 Practicum I
9
: := Variables := Need to be declared: VAR
Name Type VAR X : CARDINAL; Change the value (assignment) X := 451; : := := 20/11/2018 Practicum I
10
Variables Single types: Why the name « single types »?
Characters (‘a’) : CHAR Integers (-40) : INTEGER Reals (2.7) : REAL Pos number (3) : CARDINAL Logical (TRUE/FALSE) : BOOLEAN Why the name « single types »? 20/11/2018 Practicum I
11
:= Variables Keyword Declarations Assignments MODULE demo; VAR
a : CHAR; b : INTEGER; c : REAL; d : CARDINAL; e : BOOLEAN: BEGIN a := ‘z’; b := -345; c := 0.35E-3; d := 345; e := FALSE; END demo. Declarations Assignments := 20/11/2018 Practicum I
12
Variables vs. Literals Variable can have several values (not at one time) Literals are the values themselves: -3 123 ‘z’ 4.342 TRUE 20/11/2018 Practicum I
13
Working with numbers INTEGER, CARDINAL and REAL Operators:
Sum using + Substract using – Multiply using * Divide using DIV for CARDINAL and INTEGER / for REAL Modulo using MOD 20/11/2018 Practicum I
14
Working with numbers MODULE demo; VAR a, b : INTEGER; d : REAL;
f : CARDINAL; BEGIN a := -3; b := a * 2; d := 5.0 / 3.0; f := 5 DIV 3; END demo. 20/11/2018 Practicum I
15
Conversions Numbers can not be mixed! CARDINAL REAL INTEGER
Conversion is needed TRUNC(real) => CARDINAL FLOAT(cardinal/integer) => REAL 20/11/2018 Practicum I
16
BOOLEAN algebra Built-in type: BOOLEAN 2 values: TRUE, FALSE
OPERATORS: Logical : AND, OR, NOT Comparison : <, >, =, <=, >=, <>, # 20/11/2018 Practicum I
17
BOOLEAN algebra MODULE demo; VAR a, b : INTEGER; c, d : BOOLEAN; BEGIN
c := a > b; d := NOT c; END demo. 20/11/2018 Practicum I
18
Precedence rules Define which operator is evaluated first.
Multiplication, division, sum, … 13 DIV 4 = 3 = TRUE Is this correct? What is the value then? Use brackets to ensure the order of evalution => ((13 DIV 4) = 3) = TRUE) 20/11/2018 Practicum I
19
Characters Built-in type: CHAR
Values: all the entries of the ASCII table Character literals have to be surrounded by single quotes : ‘z’ How can we represent text… later 20/11/2018 Practicum I
20
= Constant Keyword CONST Usage identical to variable
Assignment is impossible MODULE demo; CONST pi = ; code = 9342; = 20/11/2018 Practicum I
21
Constant What is the type of the constant?
Can I use constant together with variables and literals? MODULE demo; CONST Pi = ; VAR x, r : REAL; BEGIN r := 5.0; x := 2.0 * Pi * r; END demo. 20/11/2018 Practicum I
22
Enumeration First flexible type: defined by you
Explicit definition of the possible values of a variable during the declaration VAR x : ( value1, value2, …, valueN); Variable x can only have the values listed between brackets 20/11/2018 Practicum I
23
Enumeration MODULE demo; VAR
continent : ( europe, asia, africa, antartica, america); BEGIN continent := antartica; END demo. Danger: values of enumeration can not be used as name for other constants or variable 20/11/2018 Practicum I
24
.. Subrange Equivalent to interval in mathematics x [0, 10]
Again defined by you during the declaration VAR x : [ ]; c : [’a’ .. ‘d’]; Not for real valued intervals!! .. 20/11/2018 Practicum I
25
Subrange MODULE demo; VAR BEGIN END demo. a, b, c : [2..15]; a := 2;
c := a * b; END demo. 20/11/2018 Practicum I
26
Structured types Most types are built-in: Self defined types:
CHAR, REAL, BOOLEAN, CARDINAL, INTEGER Self defined types: Enumeration Subrange Until now simple types i.e. one variable = one value at a time 20/11/2018 Practicum I
27
Structured types Store more than one value per variable
RECORD: possible to combine different types in one variable ARRAY: represent several values of the same type in one variable 20/11/2018 Practicum I
28
Record Delcaration VAR name: RECORD field1 : type1; fieldN : typeN;
. fieldN : typeN; END; 20/11/2018 Practicum I
29
Record Declaration VAR date : RECORD END; day : [1..31];
month : [1..12]; year : [ ]; END; 20/11/2018 Practicum I
30
Record Since a record contains several values we use the field name to access the value: record.field_name date.day := 8; date.month := 10; date.year := 2002; . 20/11/2018 Practicum I
31
Record Possible? VAR point1, point2 : RECORD x, y : REAL; END; BEGIN
point1.x := 5.4; point1.y := 2.3; point2 := point1; Possible? 20/11/2018 Practicum I
32
Array Declaration VAR name : ARRAY index_type OF type;
Array contains one or more values depending on the index_type x : ARRAY [1..10] OF REAL; 20/11/2018 Practicum I
33
Array As with RECORDs with need more work to take one value out of an array => indexing array[position] x[8] := 4.3; x[2] := x[8] + 1.2; 20/11/2018 Practicum I
34
Array VAR price : ARRAY (junior, normal, maxi) OF REAL; BEGIN
price[normal] := 2.25; price[maxi] := 3.00; 20/11/2018 Practicum I
35
Array VAR taste : ARRAY (bread, rice) OF (bad, normal, good);
class : ARRAY(john, alan) OF RECORD age : [0..200]; weight : REAL; END; BEGIN taste[bread] := good; taste[rice] := good; class[john].age := 25; 20/11/2018 Practicum I
36
Array Respect the index type Arrays can not be copied in 1 step
VAR x : ARRAY [3..10] OF BOOLEAN; BEGIN x[1] := TRUE; Arrays can not be copied in 1 step VAR x, y : ARRAY [3..10] OF BOOLEAN; x := y; 20/11/2018 Practicum I
37
Strings Character can be represented using the built-in CHAR type
One character at a time There is no seperate type in Modula 2 for strings of characters! VAR text : ARRAY [1..30] OF CHAR; 20/11/2018 Practicum I
38
Writing on screen Writing on the computer screen can be done using the following procedures String : WrStr(x); INTEGER: WrInt(x, 0); REAL: WrReal(x, 7, 0); CARDINAL: WrCard(x, 0); BOOLEAN: WrBool(x); CHAR: WrChar(x); 20/11/2018 Practicum I
39
N-dimensional array Modula 2 arrays can have more than one index => more than one dimension VAR x : ARRAY [1..5] OF ARRAY [1..3] OF REAL; BEGIN x[5][2] := 5.0; 20/11/2018 Practicum I
40
N-dimensional array Declaration short hand for n-dim arrays:
x : ARRAY type1 OF ARRAY type2 OF ARRAY type3 OF type x : ARRAY type1,type2,type3 OF type Indexing short hand for n-dim arrays: x[3][3][4] x[3,3,4] 20/11/2018 Practicum I
41
= TYPE Modula 2 comes with built-in types
You can also define new types MODULE demo; TYPE Mp3 = RECORD name : ARRAY [1..40] OF CHAR; length : [ ]; END; = 20/11/2018 Practicum I
42
TYPE MODULE demo; TYPE VAR song : Mp3; BEGIN
Mp3 = RECORD name : ARRAY [1..40] OF CHAR; length : [ ]; END; VAR song : Mp3; BEGIN song.name := "4 seasons – winter"; END demo. 20/11/2018 Practicum I
43
Control instruction Normal program execution
Top down Line by line Statements that influence the execution of the program 3 groups: Branches Loops Procedures 20/11/2018 Practicum I
44
IF Choose an alternative based on result of the boolean expression IF boolean_express1 THEN statements… ELIF boolean_express2 THEN ELSE END; Optional 20/11/2018 Practicum I
45
IF MODULE demo; FROM IO IMPORT WrStr; VAR x : CARDINAL; BEGIN x := 3;
IF (x < 5) THEN WrStr(" x is smaller than 5"); END; END demo. 20/11/2018 Practicum I
46
IF MODULE demo; FROM IO IMPORT WrStr; VAR x : CARDINAL; BEGIN x := 3;
IF (x < 5) THEN WrStr(" x is smaller than 5"); ELSE WrStr(" x is >= 5"); END; END demo. 20/11/2018 Practicum I
47
IF MODULE demo; FROM IO IMPORT WrStr; VAR x : CARDINAL; BEGIN x := 3;
IF (x < 5) THEN WrStr(" x is smaller than 5"); ELIF (x > 5) THEN WrStr(" x is bigger than 5"); ELSE WrStr("x equals 5"); END; END demo. 20/11/2018 Practicum I
48
CASE Optional Choose one alternative using a label, no test!!
CASE expression OF label1, label2 : statements… | label3 : staments… | ELSE statements… END; Optional 20/11/2018 Practicum I
49
CASE MODULE demo; VAR menu : (junior, normal, maxi); price : REAL;
BEGIN menu := junior; CASE menu OF junior : price := 1.25; | normal : price := 2.25; | maxi : price := 3.00; | ELSE price := ; END; END demo. 20/11/2018 Practicum I
50
FOR Loop a fixed number of times using a counter variable
FOR counter := start TO stop BY step DO statements… END; BY step is optional 20/11/2018 Practicum I
51
FOR The counter will be changed automatically by the FOR loop
If BY step is omitted a default step of 1 is used start < stop if step is > 0 FOR counter := 10 TO 1 BY –1 DO 20/11/2018 Practicum I
52
FOR MODULE demo; VAR i : CARDINAL; sum : CARDINAL; BEGIN sum := 0;
FOR i := 1 TO 20 DO sum := sum + i; END; END demo. 20/11/2018 Practicum I
53
WHILE Loop as long as the condition return TRUE
WHILE boolean_express DO statements… END; 20/11/2018 Practicum I
54
WHILE MODULE demo; VAR i : CARDINAL; sum : CARDINAL; BEGIN sum := 0;
WHILE (i <= 20) DO sum := sum + i; INC(i); END; END demo. 20/11/2018 Practicum I
55
REPEAT ! Loop until condition becomes TRUE REPEAT statements…
UNTIL boolean_express; Always at least 1 loop!! ! 20/11/2018 Practicum I
56
REPEAT MODULE demo; VAR i : CARDINAL; sum : CARDINAL; BEGIN sum := 0;
INC(sum, i); INC(i); UNTIL (i > 20); END demo. 20/11/2018 Practicum I
57
LOOP The most basic loop LOOP statements… END;
No counter No stop condition LOOP statements… END; Use EXIT to escape the loop 20/11/2018 Practicum I
58
LOOP MODULE demo; VAR i : CARDINAL; sum : CARDINAL; BEGIN sum := 0;
INC(sum, i); INC(i); IF (i >20) THEN EXIT; END; END demo. 20/11/2018 Practicum I
59
PROCEDURE Frequently used statement can be seperated and put into a procedure => code reuse In Modula 2 a procedure a is little program on its own Difference with program: Has a name Possibly some input values Possibly some output values 20/11/2018 Practicum I
60
PROCEDURE 2 aspect: Definition: which computation are being performed, which inputs and outputs Procedure call: correct use of the procedure in the program 20/11/2018 Practicum I
61
PROCEDURE Definition Name Number of parameters Type of each parameter
Does the procedure return a value: Type of the value return value 20/11/2018 Practicum I
62
PROCEDURE 2 types of procedures
Procedures that compute a value and return it Function procedure Procedures that have a side effect (write to disk, reset a value, …) 20/11/2018 Practicum I
63
PROCEDURE PROCEDURE name (arg1 : TYPE1; arg2 : TYPE2; …);
(* Declarations just like for a MODULE *) CONST …. TYPE …. VAR …. BEGIN (* The code comes here *) …. END name; 20/11/2018 Practicum I
64
PROCEDURE PROCEDURE name (arg1 : TYPE1; arg2 : TYPE2; …) : TYPE6;
(* Declarations just like for a MODULE *) CONST …. TYPE …. VAR …. BEGIN (* The code comes here *) …. END name; 20/11/2018 Practicum I
65
PROCEDURE Keyword RETURN Used to: Jump out of the procedure
Return a value and jump out of the procedure 20/11/2018 Practicum I
66
PROCEDURE MODULE demo; PROCEDURE add ( a , b :CARDINAL) : CARDINAL;
BEGIN RETURN a+b; END add; VAR sum : CARDINAL; sum := add( 4, 2); END demo. 20/11/2018 Practicum I
67
PROCEDURE MODULE demo; FROM IO IMPORT WrStr; PROCEDURE odd( x : REAL);
BEGIN IF (x < 4.5) THEN RETURN; END; WrStr("x is not < 4.5"); END odd; odd(3.); odd(5.5); END demo. 20/11/2018 Practicum I
68
SCOPING Procedure are little programs on their own:
Own declarations : types, constants, variables and even other procedures Which variables, constants and procedure can one use inside procedure? Scoping rules 20/11/2018 Practicum I
69
SCOPING Global vs. Local delcarations Top down Modules
Global ~ defined in the main part of the module Local ~ defined in procedures Top down Modules Hiding phenomenon 20/11/2018 Practicum I
70
SCOPING MODULE demo; VAR v : CHAR; CONST c = 0; PROCEDURE p; z : CHAR;
…(* v, c, z, p *) END p; PROCEDURE q; v = 6; …(* v, c, p, q *) END q; BEGIN END demo. 20/11/2018 Practicum I
71
VAL/VAR MODULE demo; PROCEDURE reset(c : REAL); BEGIN c := 0.0;
END zero; VAR r : REAL; r := 99.99; reset(r); END demo. 20/11/2018 Practicum I
72
VAL/VAR The previous program does not work, why?
Parameter passing mechanism: Makes copy of the arguments (value parameter VAL) Execute the procedure code VAR parameters are not copied! 20/11/2018 Practicum I
73
VAL/VAR MODULE demo; PROCEDURE zero (VAR c : REAL); BEGIN c := 0.0;
END zero; VAR x : REAL BEGIN reset(x); END demo. Works as was expected! 20/11/2018 Practicum I
74
VAL/VAR VAR parameters are not copied but a reference to the original variable is passed instead VAR parameter required variables during the procedure call, why? 20/11/2018 Practicum I
75
OPEN ARRAY Passing arrays as parameters is not easy?!
PROCEDURE p(a : ARRAY [1..10] OF REAL); To make it work: Declare a new type first Use type in declaration TYPE Arr = ARRAY [1..10] OF REAL; PROCEDURE p(a :Arr); 20/11/2018 Practicum I
76
OPEN ARRAY Not convenient: Solution : open array parameters
Declaration Not flexible in terms of the size arrays passed Solution : open array parameters 20/11/2018 Practicum I
77
OPEN ARRAY Advantage: ARRAY OF type PROCEDURE p(a: ARRAY OF REAL);
No need to declare a type Flexible in terms of size ARRAY OF type PROCEDURE p(a: ARRAY OF REAL); Procedure p can accept arrays of any size 20/11/2018 Practicum I
78
OPEN ARRAY All arrays start at index 0
HIGH returns the index of the last position 20/11/2018 Practicum I
79
OPEN ARRAY PROCEDURE sum(a : ARRAY OF REAL): REAL; VAR i : CARDINAL;
tot : REAL; BEGIN tot := 0.0; FOR i := 0 TO HIGH(a) DO tot := tot + a[i]; END; RETURN tot; END sum; 20/11/2018 Practicum I
80
OPEN ARRAY VAR PROCEDURE myWrStr( str : ARRAY OF CHAR); BEGIN
firstAr : ARRAY [30..35] OF CHAR; secondAr : ARRAY [1..10] OF CHAR; PROCEDURE myWrStr( str : ARRAY OF CHAR); i : CARDINAL; BEGIN FOR i := 0 TO HIGH(str) DO WrChar(str[i]); END; END myWrStr; 20/11/2018 Practicum I
81
RECURSION f(x) = g(f(x)) Example: factorial
fac(n) := n * fac(n-1); (n>1) fac(n) := 1; (n <=1) PROCEDURE fac(n : CARDINAL): CARDINAL; BEGIN IF (n <= 1) THEN RETURN n * fac(n-1); ELSE RETURN 1; END; END fac; 20/11/2018 Practicum I
82
POINTERS Pointers are variables containing memory addresses
Declaration VAR a : POINTER TO INTEGER; a contains the address of a memory block which contains an INTEGER value. 20/11/2018 Practicum I
83
POINTER Adresses of memory locations are not interesting
The value stored at the memory location is => dereference VAR a : POINTER TO INTEGER; BEGIN … a^ := -3; 20/11/2018 Practicum I
84
POINTERS Pointers are used for dynamic memory allocation! But pointers do not do the allocation. There are special Modula2 procedures to work with memory: IMPOR from the SYSTEM module NEW used to ask memory DISPOSE to release the memory you got 20/11/2018 Practicum I
85
Pointers: DEMO MODULE memory; FROM SYSTEM IMPORT NEW, DISPOSE;
FROM IO IMPORT WrStr, WrLn; VAR i_ptr : POINTER TO INTEGER; i : INTEGER; BEGIN WrStr("Start"); WrLn; (* We reserve some memory for 1 integer *) NEW(i_ptr); i_ptr^ := 9; i := i_ptr^; (* We tell the computer we will not use this memory anymore *) DISPOSE(i_ptr); WrStr("Stop"); WrLn; END memory. 20/11/2018 Practicum I
86
POINTERS Dynamic memory allocation is the main reason for using pointers. Special structures are used to do this: Linked lists Trees These are RECURSIVE DATA STRUCTURES 20/11/2018 Practicum I
87
POINTERS Linked list Tree data : REAL; next : LinkPtr; END;
TYPE LinkPtr = POINTER TO LinkRc; LinkRc = RECORD data : REAL; next : LinkPtr; END; Tree NodePtr = POINTER TO NodeRc; NodeRc = RECORD left, right : NodePtr; 20/11/2018 Practicum I
88
POINTERS These structures can be contected to each other!
Thereby allowing us to build big structures where each element contains data! In this way we can use as much memory as we need! And this without knowing in advance how much memory we would need. 20/11/2018 Practicum I
89
Pointers: memory allocation
We can thus ask memory explicitly using NEW but: the computer may run out of memory!!! We can know that when NEW returns NIL as value for our pointer 20/11/2018 Practicum I
90
Pointers: memory allocation (2)
In order to save memory we have to recycle the memory we do not use anymore we have to deallocate the memory using DISPOSE we have to be careful and avoid using memory we deallocated!! 20/11/2018 Practicum I
91
MODULES 20/11/2018 Practicum I
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.