TOPIC I: VAL/VAR
procedure parameter by VAL/VAR.
Verschil tussen Call by Value/Reference
study the demo: parameters are changed IN the procedure.
S1: Reset procedures
Giving variables zero as a value is something that has to be done
quite frequently. Write a procedure that takes an INTEGER as parameter
and sets it to zero. Call this procedure zero. Example
Make a more generic procedure which can assign arbitrary values to a
This procedure has 2 parameters: the variable whose value has to be set
and a the value. Call this procedure set. You should be
to make zero using set but not the
Example set(x, -99);
S2: Your own version of INC
Modula2 has a built-in INC procedure which is quite flexible. Make a
that does the same but only for CARDINALs. Call it myINC.
S3: Swap procedure
VAR x, y: CARDINAL;
Make a procedure Swap that 'swaps' (verwisselen) two
x becomes y and vice versa.
x := 7;
y := 10;
WrStr(" x = ");WrCard(x, 0);WrLn;
WrStr(" y = ");WrCard(y, 0);WrLn;
S4: DIV & MOD
Make a procedure that returns both the division (deling)
the modulo (rest) of two cardinals.
What is the output of the following program?
FROM IO IMPORT WrLn, WrCard, RdCard;
PROCEDURE proc1(VAR a1:CARDINAL; a2: CARDINAL):CARDINAL;
a1 := a2 + 7;
a2 := a2 - 7;
RETURN a1 * a2 - 4;
PROCEDURE proc2(b1: CARDINAL; VAR b2:CARDINAL):BOOLEAN;
VAR toReturn: BOOLEAN;
b3 := b2;
b2 := b1 MOD b3;
b1 := b1 / b3;
IF (b3 = 0) THEN
toReturn := TRUE;
toReturn := FALSE;
VAR x1, x2, x3: CARDINAL;
x1 := 3;
x2 := 11;
x3 := proc1(x1, x2);
IF proc2(x3, x2) THEN
x1 := x3;
x1 := x2;