TOPIC I: VAL/VAR

Contents :

  procedure parameter by VAL/VAR.

Exercises :

Verschil tussen Call by Value/Reference

D1: VAR-demo.


S1: Reset procedures


S2: Your own version of INC


S3: Swap procedure

VAR x, y: CARDINAL;
BEGIN
  x := 7;
  y := 10;
  Swap(x, y);
  WrStr(" x = ");WrCard(x, 0);WrLn;
  WrStr(" y = ");WrCard(y, 0);WrLn;
END S3;

S4: DIV & MOD



S5:

 



X1:




H1:



H2:




T1: Output

MODULE T1;
  FROM IO IMPORT WrLn, WrCard, RdCard;

  PROCEDURE proc1(VAR a1:CARDINAL; a2: CARDINAL):CARDINAL;
  BEGIN
    a1 := a2 + 7;
    a2 := a2 - 7;
    RETURN a1 * a2 - 4;
  END proc1;

  PROCEDURE proc2(b1: CARDINAL; VAR b2:CARDINAL):BOOLEAN;
    VAR toReturn: BOOLEAN;
    b3:CARDINAL;
  BEGIN
    b3 := b2;
    b2 := b1 MOD b3;
    b1 := b1 / b3;
    IF (b3 = 0) THEN
        toReturn := TRUE;
    ELSE
        toReturn := FALSE;
    END;
    RETURN toReturn;
  END proc2;

  VAR x1, x2, x3: CARDINAL;
BEGIN
    x1 := 3;
    x2 := 11;
    x3 := proc1(x1, x2);
    IF proc2(x3, x2) THEN
        x1 := x3;
    ELSE
        x1 := x2;
    END;

    WrCard(x1, 0);
    WrLn();
    WrCard(x2, 0);
    WrLn();
    WrCard(x3, 0);
    WrLn();
END T1.