Introduction to Modula2 Johan Parent 11222020 Practicum I

  • Slides: 91
Download presentation
Introduction to Modula-2 Johan Parent 11/22/2020 Practicum I 1

Introduction to Modula-2 Johan Parent 11/22/2020 Practicum I 1

Introduction n 3 hours of total immersion into the programming language Modula-2 We start

Introduction n 3 hours of total immersion into the programming language Modula-2 We start from the very basics n Johan Parent n Johan@info. vub. ac. be u 4 K 216 u 11/22/2020 Practicum I 2

Modula 2 : why? n Why not Java, C, C++, C# ? ! n

Modula 2 : why? n Why not Java, C, C++, C# ? ! n Learning Looks like English u Very structured u Safe (easy? ) u Has all the modern features u n 11/22/2020 All programming languages are equal!! Practicum I 3

Programming n Way to control a computer Write down the program (editor) u Translate

Programming n Way to control a computer Write down the program (editor) u Translate using special program (compiler) u Execution by the computer u n Compiler is clever Translates in machine language u Reports mistakes u 11/22/2020 Practicum I 4

Modula 2 n n n 11/22/2020 Has keywords: words from the language Always in

Modula 2 n n n 11/22/2020 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… Practicum I 5

Program structure Program is called a MODULE name; n FROM …. IMPORT …; CONST

Program structure Program is called a MODULE name; n FROM …. IMPORT …; CONST pi = 3. 1459; TYPE euro = REAL; BEGIN …. (* comment *) END name. 11/22/2020 Practicum I 6

Syntax n n n 11/22/2020 Keywords completely in CAPITAL letters Spaces do not matter

Syntax n n n 11/22/2020 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. Practicum I 7

Variables n Convenient way to: Store values u Manipulate values u n 11/22/2020 Closely

Variables n Convenient way to: Store values u Manipulate values u n 11/22/2020 Closely related to the variable concept used in mathematics! Practicum I 8

Variables u Need to be declared: Name « Type « : = : VAR

Variables u Need to be declared: Name « Type « : = : VAR X : CARDINAL; u Change the value (assignment) X : = 451; 11/22/2020 Practicum I 9

Variables n Single types: Characters (‘a’) : CHAR u Integers (-40) : INTEGER u

Variables n Single types: Characters (‘a’) : CHAR u Integers (-40) : INTEGER u Reals (2. 7) : REAL u Pos number (3) : CARDINAL u Logical (TRUE/FALSE) : BOOLEAN u n 11/22/2020 Why the name « single types » ? Practicum I 10

Variables 11/22/2020 MODULE demo; VAR a : CHAR; b : INTEGER; c : REAL;

Variables 11/22/2020 MODULE demo; VAR a : CHAR; b : INTEGER; c : REAL; d : CARDINAL; e : BOOLEAN: BEGIN a : = ‘z’; b : = -345; c : = 0. 35 E-3; d : = 345; e : = FALSE; Practicum I END demo. : = 11

Variables vs. Literals n n Variable can have several values (not at one time)

Variables vs. Literals n n Variable can have several values (not at one time) Literals are the values themselves: u u u 11/22/2020 -3 123 ‘z’ 4. 342 TRUE Practicum I 12

Working with numbers n n INTEGER, CARDINAL and REAL Operators: u u u n

Working with numbers n n INTEGER, CARDINAL and REAL Operators: u u u n Divide using u u n 11/22/2020 Sum using + Substract using – Multiply using * DIV for CARDINAL and INTEGER / for REAL Modulo using MOD Practicum I 13

Working with numbers MODULE demo; VAR a, b : INTEGER; d : REAL; f

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. 11/22/2020 Practicum I 14

Conversions n Numbers can not be mixed! n CARDINAL REAL INTEGER n Conversion is

Conversions n Numbers can not be mixed! n CARDINAL REAL INTEGER n Conversion is needed u u 11/22/2020 TRUNC(real) => CARDINAL FLOAT(cardinal/integer) => REAL Practicum I 15

BOOLEAN algebra n Built-in type: BOOLEAN n 2 values: TRUE, FALSE n OPERATORS: u

BOOLEAN algebra n Built-in type: BOOLEAN n 2 values: TRUE, FALSE n OPERATORS: u u 11/22/2020 Logical : AND, OR, NOT Comparison : <, >, =, <=, >=, <>, # Practicum I 16

BOOLEAN algebra MODULE demo; VAR a, b : INTEGER; c, d : BOOLEAN; BEGIN

BOOLEAN algebra MODULE demo; VAR a, b : INTEGER; c, d : BOOLEAN; BEGIN a : = -3; b : = 33; c : = a > b; d : = NOT c; END demo. 11/22/2020 Practicum I 17

Precedence rules n Define which operator is evaluated first. u n 13 DIV 4

Precedence rules n Define which operator is evaluated first. u n 13 DIV 4 = 3 = TRUE u u n Is this correct? What is the value then? Use brackets to ensure the order of evalution => u 11/22/2020 Multiplication, division, sum, … ((13 DIV 4) = 3) = TRUE) Practicum I 18

Characters n n n Built-in type: CHAR Values: all the entries of the ASCII

Characters n n n Built-in type: CHAR Values: all the entries of the ASCII table Character literals have to be surrounded by single quotes : u n 11/22/2020 ‘z’ How can we represent text… later Practicum I 19

Constant n n n = 11/22/2020 Keyword CONST Usage identical to variable Assignment is

Constant n n n = 11/22/2020 Keyword CONST Usage identical to variable Assignment is impossible MODULE demo; CONST pi = 3. 14159; code = 9342; Practicum I 20

Constant What is the type of the constant? n Can I use constant together

Constant What is the type of the constant? n Can I use constant together with variables and literals? MODULE demo; CONST Pi = 3. 14159; VAR x, r : REAL; BEGIN r : = 5. 0; x : = 2. 0 * Pi * r; END demo. n 11/22/2020 Practicum I 21

Enumeration n n First flexible type: defined by you Explicit definition of the possible

Enumeration n n First flexible type: defined by you Explicit definition of the possible values of a variable during the declaration VAR x : ( value 1, value 2, …, value. N); n 11/22/2020 Variable x can only have the values listed between brackets Practicum I 22

Enumeration MODULE demo; VAR continent : ( europe, asia, africa, antartica, america); BEGIN continent

Enumeration MODULE demo; VAR continent : ( europe, asia, africa, antartica, america); BEGIN continent : = antartica; END demo. n 11/22/2020 Danger: values of enumeration can not be used as name for other constants or variable Practicum I 23

Subrange n Equivalent to interval in mathematics x [0, 10] Again defined by you

Subrange n Equivalent to interval in mathematics x [0, 10] Again defined by you during the declaration VAR n . . x : [0. . 10]; c : [’a’. . ‘d’]; n 11/22/2020 Not for real valued intervals!! Practicum I 24

Subrange MODULE demo; VAR a, b, c : [2. . 15]; BEGIN a :

Subrange MODULE demo; VAR a, b, c : [2. . 15]; BEGIN a : = 2; b : = 8; c : = a * b; END demo. 11/22/2020 Practicum I 25

Structured types n Most types are built-in: u n CHAR, REAL, BOOLEAN, CARDINAL, INTEGER

Structured types n Most types are built-in: u n CHAR, REAL, BOOLEAN, CARDINAL, INTEGER Self defined types: Enumeration u Subrange u n 11/22/2020 Until now simple types i. e. one variable = one value at a time Practicum I 26

Structured types n 11/22/2020 Store more than one value per variable u RECORD: possible

Structured types n 11/22/2020 Store more than one value per variable u RECORD: possible to combine different types in one variable u ARRAY: represent several values of the same type in one variable Practicum I 27

Record Delcaration VAR name: RECORD field 1 : type 1; n . . field.

Record Delcaration VAR name: RECORD field 1 : type 1; n . . field. N : type. N; END; 11/22/2020 Practicum I 28

Record Declaration VAR n date : RECORD day : [1. . 31]; month :

Record Declaration VAR n date : RECORD day : [1. . 31]; month : [1. . 12]; year : [1900. . 3000]; END; 11/22/2020 Practicum I 29

Record n Since a record contains several values we use the field name to

Record n Since a record contains several values we use the field name to access the value: u . 11/22/2020 . record field_name date. day : = 8; date. month : = 10; date. year : = 2002; Practicum I 30

Record VAR point 1, point 2 : RECORD x, y : REAL; END; BEGIN

Record VAR point 1, point 2 : RECORD x, y : REAL; END; BEGIN point 1. x : = 5. 4; point 1. y : = 2. 3; point 2 : = point 1; 11/22/2020 Practicum I 31

Array Declaration VAR name : ARRAY index_type OF type; n Array contains one or

Array Declaration VAR name : ARRAY index_type OF type; n Array contains one or more values depending on the index_type VAR x : ARRAY [1. . 10] OF REAL; n 11/22/2020 Practicum I 32

Array n As with RECORDs with need more work to take one value out

Array n As with RECORDs with need more work to take one value out of an array => indexing u [ array position ] x[8] : = 4. 3; x[2] : = x[8] + 1. 2; 11/22/2020 Practicum I 33

Array VAR price : ARRAY (junior, normal, maxi) OF REAL; BEGIN price[normal] : =

Array VAR price : ARRAY (junior, normal, maxi) OF REAL; BEGIN price[normal] : = 2. 25; price[maxi] : = 3. 00; 11/22/2020 Practicum I 34

Array VAR taste : ARRAY (bread, rice) OF (bad, normal, good); class : ARRAY(john,

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; 11/22/2020 Practicum I 35

Array n Respect the index type VAR x : ARRAY [3. . 10] OF

Array n Respect the index type VAR x : ARRAY [3. . 10] OF BOOLEAN; BEGIN x[1] : = TRUE; n Arrays can not be copied in 1 step VAR x, y : ARRAY [3. . 10] OF BOOLEAN; BEGIN x : = y; 11/22/2020 Practicum I 36

Strings n Character can be represented using the built-in CHAR type u One character

Strings n Character can be represented using the built-in CHAR type u One character at a time There is no seperate type in Modula 2 for strings of characters! VAR text : ARRAY [1. . 30] OF CHAR; n 11/22/2020 Practicum I 37

Writing on screen n Writing on the computer screen can be done using the

Writing on screen n Writing on the computer screen can be done using the following procedures String : Wr. Str(x); u INTEGER: Wr. Int(x, 0); u REAL: Wr. Real(x, 7, 0); u CARDINAL: Wr. Card(x, 0); u BOOLEAN: Wr. Bool(x); u CHAR: Wr. Char(x); u 11/22/2020 Practicum I 38

N-dimensional array n Modula 2 arrays can have more than one index => more

N-dimensional array n 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; 11/22/2020 Practicum I 39

N-dimensional array n Declaration short hand for n-dim arrays: u x : ARRAY type

N-dimensional array n Declaration short hand for n-dim arrays: u x : ARRAY type 1 OF ARRAY type 2 OF ARRAY type 3 OF type u n x : ARRAY type 1, type 2, type 3 OF type Indexing short hand for n-dim arrays: u x[3][3][4] u 11/22/2020 x[3, 3, 4] Practicum I 40

TYPE n Modula 2 comes with built-in types You can also define new types

TYPE n Modula 2 comes with built-in types You can also define new types MODULE demo; TYPE n = 11/22/2020 Mp 3 = RECORD name : ARRAY [1. . 40] OF CHAR; length : [1. . 3600]; END; Practicum I 41

TYPE MODULE demo; TYPE Mp 3 = RECORD name : ARRAY [1. . 40]

TYPE MODULE demo; TYPE Mp 3 = RECORD name : ARRAY [1. . 40] OF CHAR; length : [1. . 3600]; END; VAR song : Mp 3; BEGIN song. name : = "4 seasons – winter"; END demo. 11/22/2020 Practicum I 42

Control instruction n Normal program execution u u n n Statements that influence the

Control instruction n Normal program execution u u n n Statements that influence the execution of the program 3 groups: u u u 11/22/2020 Top down Line by line Branches Loops Procedures Practicum I 43

IF Choose an alternative based on result of the boolean expression IF boolean_express 1

IF Choose an alternative based on result of the boolean expression IF boolean_express 1 THEN statements… ELIF boolean_express 2 THEN statements… ELSE statements… END; n 11/22/2020 Practicum I 44

IF MODULE demo; FROM IO IMPORT Wr. Str; VAR x : CARDINAL; BEGIN x

IF MODULE demo; FROM IO IMPORT Wr. Str; VAR x : CARDINAL; BEGIN x : = 3; IF (x < 5) THEN Wr. Str(" x is smaller than 5"); END demo. 11/22/2020 Practicum I 45

IF MODULE demo; FROM IO IMPORT Wr. Str; VAR x : CARDINAL; BEGIN x

IF MODULE demo; FROM IO IMPORT Wr. Str; VAR x : CARDINAL; BEGIN x : = 3; IF (x < 5) THEN Wr. Str(" x is smaller than 5"); ELSE Wr. Str(" x is >= 5"); END demo. 11/22/2020 Practicum I 46

IF 11/22/2020 MODULE demo; FROM IO IMPORT Wr. Str; VAR x : CARDINAL; BEGIN

IF 11/22/2020 MODULE demo; FROM IO IMPORT Wr. Str; VAR x : CARDINAL; BEGIN x : = 3; IF (x < 5) THEN Wr. Str(" x is smaller than 5"); ELIF (x > 5) THEN Wr. Str(" x is bigger than 5"); ELSE Wr. Str("x equals 5"); END demo. Practicum I 47

CASE n Choose one alternative using a label, no test!! CASE expression OF label

CASE n Choose one alternative using a label, no test!! CASE expression OF label 1, label 2 : statements… | label 3 : staments… | ELSE statements… END; 11/22/2020 Practicum I 48

CASE MODULE demo; VAR menu : (junior, normal, maxi); price : REAL; BEGIN menu

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 : = -99. 999; END demo. 11/22/2020 Practicum I 49

FOR n Loop a fixed number of times using a counter variable FOR counter

FOR n Loop a fixed number of times using a counter variable FOR counter : = start TO stop BY step DO statements… END; n 11/22/2020 BY step is optional Practicum I 50

FOR n n n The counter will be changed automatically by the FOR loop

FOR n n n 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 11/22/2020 Practicum I 51

FOR MODULE demo; VAR i : CARDINAL; sum : CARDINAL; BEGIN sum : =

FOR MODULE demo; VAR i : CARDINAL; sum : CARDINAL; BEGIN sum : = 0; FOR i : = 1 TO 20 DO sum : = sum + i; END demo. 11/22/2020 Practicum I 52

WHILE n Loop as long as the condition return TRUE WHILE boolean_express DO statements…

WHILE n Loop as long as the condition return TRUE WHILE boolean_express DO statements… END; 11/22/2020 Practicum I 53

WHILE MODULE demo; VAR i : CARDINAL; sum : CARDINAL; BEGIN sum : =

WHILE MODULE demo; VAR i : CARDINAL; sum : CARDINAL; BEGIN sum : = 0; i : = 1; WHILE (i <= 20) DO sum : = sum + i; INC(i); END demo. 11/22/2020 Practicum I 54

REPEAT n Loop until condition becomes TRUE REPEAT statements… UNTIL boolean_express; n 11/22/2020 Always

REPEAT n Loop until condition becomes TRUE REPEAT statements… UNTIL boolean_express; n 11/22/2020 Always at least 1 loop!! Practicum I 55

REPEAT 11/22/2020 MODULE demo; VAR i : CARDINAL; sum : CARDINAL; BEGIN sum :

REPEAT 11/22/2020 MODULE demo; VAR i : CARDINAL; sum : CARDINAL; BEGIN sum : = 0; i : = 1; REPEAT INC(sum, i); INC(i); UNTIL (i > 20); END demo. Practicum I 56

LOOP n The most basic loop No counter u No stop condition u LOOP

LOOP n The most basic loop No counter u No stop condition u LOOP statements… END; n 11/22/2020 Use EXIT to escape the loop Practicum I 57

LOOP 11/22/2020 MODULE demo; VAR i : CARDINAL; sum : CARDINAL; BEGIN sum :

LOOP 11/22/2020 MODULE demo; VAR i : CARDINAL; sum : CARDINAL; BEGIN sum : = 0; i : = 1; LOOP INC(sum, i); INC(i); IF (i >20) THEN EXIT; END; END demo. Practicum I 58

PROCEDURE n n n Frequently used statement can be seperated and put into a

PROCEDURE n n n 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 u Possibly some input values u Possibly some output values u 11/22/2020 Practicum I 59

PROCEDURE n 11/22/2020 2 aspect: u Definition: which computation are being performed, which inputs

PROCEDURE n 11/22/2020 2 aspect: u Definition: which computation are being performed, which inputs and outputs u Procedure call: correct use of the procedure in the program Practicum I 60

PROCEDURE n Definition Name u Number of parameters u Type of each parameter u

PROCEDURE n Definition Name u Number of parameters u Type of each parameter u u Does the procedure return a value: « Type of the value return value 11/22/2020 Practicum I 61

PROCEDURE n 2 types of procedures u Procedures that compute a value and return

PROCEDURE n 2 types of procedures u Procedures that compute a value and return it « Function procedure u 11/22/2020 Procedures that have a side effect (write to disk, reset a value, …) Practicum I 62

PROCEDURE name (arg 1 : TYPE 1; arg 2 : TYPE 2; …); (*

PROCEDURE name (arg 1 : TYPE 1; arg 2 : TYPE 2; …); (* Declarations just like for a MODULE *) CONST …. TYPE …. VAR …. BEGIN (* The code comes here *) …. END name; 11/22/2020 Practicum I 63

PROCEDURE name (arg 1 : TYPE 1; arg 2 : TYPE 2; …) :

PROCEDURE name (arg 1 : TYPE 1; arg 2 : TYPE 2; …) : TYPE 6; (* Declarations just like for a MODULE *) CONST …. TYPE …. VAR …. BEGIN (* The code comes here *) …. END name; 11/22/2020 Practicum I 64

PROCEDURE 11/22/2020 n Keyword RETURN n Used to: u Jump out of the procedure

PROCEDURE 11/22/2020 n Keyword RETURN n Used to: u Jump out of the procedure u Return a value and jump out of the procedure Practicum I 65

PROCEDURE MODULE demo; PROCEDURE add ( a , b : CARDINAL) : CARDINAL; BEGIN

PROCEDURE MODULE demo; PROCEDURE add ( a , b : CARDINAL) : CARDINAL; BEGIN RETURN a+b; END add; VAR sum : CARDINAL; BEGIN sum : = add( 4, 2); END demo. 11/22/2020 Practicum I 66

PROCEDURE MODULE demo; FROM IO IMPORT Wr. Str; PROCEDURE odd( x : REAL); BEGIN

PROCEDURE MODULE demo; FROM IO IMPORT Wr. Str; PROCEDURE odd( x : REAL); BEGIN IF (x < 4. 5) THEN RETURN; END; Wr. Str("x is not < 4. 5"); END odd; 11/22/2020 BEGIN odd(3. ); odd(5. 5); END demo. Practicum I 67

SCOPING n Procedure are little programs on their own: u Own declarations : types,

SCOPING n Procedure are little programs on their own: u Own declarations : types, constants, variables and even other procedures Which variables, constants and procedure can one use inside procedure? Scoping rules n 11/22/2020 Practicum I 68

SCOPING n Global vs. Local delcarations Global ~ defined in the main part of

SCOPING n Global vs. Local delcarations Global ~ defined in the main part of the module u Local ~ defined in procedures u n n n 11/22/2020 Top down Modules Hiding phenomenon Practicum I 69

SCOPING MODULE demo; VAR v : CHAR; CONST c = 0; PROCEDURE p; VAR

SCOPING MODULE demo; VAR v : CHAR; CONST c = 0; PROCEDURE p; VAR z : CHAR; …(* v, c, z, p *) END p; PROCEDURE q; CONST v = 6; …(* v, c, p, q *) END q; BEGIN END demo. 11/22/2020 Practicum I 70

VAL/VAR MODULE demo; PROCEDURE reset(c : REAL); BEGIN c : = 0. 0; END

VAL/VAR MODULE demo; PROCEDURE reset(c : REAL); BEGIN c : = 0. 0; END zero; VAR r : REAL; BEGIN r : = 99. 99; reset(r); END demo. 11/22/2020 Practicum I 71

VAL/VAR n n The previous program does not work, why? Parameter passing mechanism: Makes

VAL/VAR n n The previous program does not work, why? Parameter passing mechanism: Makes copy of the arguments (value parameter VAL) u Execute the procedure code u n 11/22/2020 VAR parameters are not copied! Practicum I 72

VAL/VAR MODULE demo; PROCEDURE zero (VAR c : REAL); BEGIN c : = 0.

VAL/VAR MODULE demo; PROCEDURE zero (VAR c : REAL); BEGIN c : = 0. 0; END zero; VAR x : REAL BEGIN reset(x); END demo. n Works as was expected! 11/22/2020 Practicum I 73

VAL/VAR n n 11/22/2020 VAR parameters are not copied but a reference to the

VAL/VAR n n 11/22/2020 VAR parameters are not copied but a reference to the original variable is passed instead VAR parameter required variables during the procedure call, why? Practicum I 74

OPEN ARRAY Passing arrays as parameters is not easy? ! PROCEDURE p(a : ARRAY

OPEN ARRAY Passing arrays as parameters is not easy? ! PROCEDURE p(a : ARRAY [1. . 10] OF REAL); n n To make it work: Declare a new type first u Use type in declaration u TYPE Arr = ARRAY [1. . 10] OF REAL; PROCEDURE p(a : Arr); 11/22/2020 Practicum I 75

OPEN ARRAY n Not convenient: Declaration u Not flexible in terms of the size

OPEN ARRAY n Not convenient: Declaration u Not flexible in terms of the size arrays passed u n 11/22/2020 Solution : open array parameters Practicum I 76

OPEN ARRAY n Advantage: No need to declare a type u Flexible in terms

OPEN ARRAY n Advantage: No need to declare a type u Flexible in terms of size u n ARRAY OF type PROCEDURE p(a: ARRAY OF REAL); n Procedure p can accept arrays of any size 11/22/2020 Practicum I 77

OPEN ARRAY n n All arrays start at index 0 HIGH returns the index

OPEN ARRAY n n All arrays start at index 0 HIGH returns the index of the last position 11/22/2020 Practicum I 78

OPEN ARRAY PROCEDURE sum(a : ARRAY OF REAL): REAL; VAR i : CARDINAL; tot

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; 11/22/2020 Practicum I 79

OPEN ARRAY VAR first. Ar : ARRAY [30. . 35] OF CHAR; second. Ar

OPEN ARRAY VAR first. Ar : ARRAY [30. . 35] OF CHAR; second. Ar : ARRAY [1. . 10] OF CHAR; PROCEDURE my. Wr. Str( str : ARRAY OF CHAR); VAR i : CARDINAL; BEGIN FOR i : = 0 TO HIGH(str) DO Wr. Char(str[i]); END my. Wr. Str; 11/22/2020 Practicum I 80

RECURSION n n f(x) = g(f(x)) Example: factorial fac(n) : = n * fac(n-1);

RECURSION n n 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 fac; 11/22/2020 Practicum I 81

POINTERS Pointers are variables containing memory addresses n Declaration VAR a : POINTER TO

POINTERS Pointers are variables containing memory addresses n Declaration VAR a : POINTER TO INTEGER; n a contains the address of a memory block which contains an INTEGER value. n 11/22/2020 Practicum I 82

POINTER n n Adresses of memory locations are not interesting The value stored at

POINTER n n Adresses of memory locations are not interesting The value stored at the memory location is => dereference VAR a : POINTER TO INTEGER; BEGIN … a^ : = -3; 11/22/2020 Practicum I 83

POINTERS n n Pointers are used for dynamic memory allocation! But pointers do not

POINTERS n n Pointers are used for dynamic memory allocation! But pointers do not do the allocation. There are special Modula 2 procedures to work with memory: IMPOR from the SYSTEM module u NEW used to ask memory u DISPOSE to release the memory you got u 11/22/2020 Practicum I 84

Pointers: DEMO MODULE memory; FROM SYSTEM IMPORT NEW, DISPOSE; FROM IO IMPORT Wr. Str,

Pointers: DEMO MODULE memory; FROM SYSTEM IMPORT NEW, DISPOSE; FROM IO IMPORT Wr. Str, Wr. Ln; VAR i_ptr : POINTER TO INTEGER; i : INTEGER; BEGIN Wr. Str("Start"); Wr. Ln; (* 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); Wr. Str("Stop"); Wr. Ln; END memory. 11/22/2020 Practicum I 85

POINTERS n n Dynamic memory allocation is the main reason for using pointers. Special

POINTERS n n Dynamic memory allocation is the main reason for using pointers. Special structures are used to do this: Linked lists u Trees u n 11/22/2020 These are RECURSIVE DATA STRUCTURES Practicum I 86

POINTERS n Linked list TYPE Link. Ptr = POINTER TO Link. Rc; Link. Rc

POINTERS n Linked list TYPE Link. Ptr = POINTER TO Link. Rc; Link. Rc = RECORD data : REAL; next : Link. Ptr; END; n Tree TYPE Node. Ptr = POINTER TO Node. Rc; Node. Rc = RECORD data : REAL; left, right : Node. Ptr; END; 11/22/2020 Practicum I 87

POINTERS n n n 11/22/2020 These structures can be contected to each other! Thereby

POINTERS n n n 11/22/2020 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. Practicum I 88

Pointers: memory allocation n 11/22/2020 We can thus ask memory explicitly using NEW but:

Pointers: memory allocation n 11/22/2020 We can thus ask memory explicitly using NEW but: u the computer may run out of memory!!! u We can know that when NEW returns NIL as value for our pointer Practicum I 89

Pointers: memory allocation (2) n 11/22/2020 In order to save memory we have to

Pointers: memory allocation (2) n 11/22/2020 In order to save memory we have to recycle the memory we do not use anymore u we have to deallocate the memory using DISPOSE u we have to be careful and avoid using memory we deallocated!! Practicum I 90

MODULES 11/22/2020 Practicum I 91

MODULES 11/22/2020 Practicum I 91