Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2.7 Modula 2 Procedures. Summary Subroutines Procedures –Block structured languages –Scope rules –Memory Management Procedure Parameters –Value.

Similar presentations


Presentation on theme: "Chapter 2.7 Modula 2 Procedures. Summary Subroutines Procedures –Block structured languages –Scope rules –Memory Management Procedure Parameters –Value."— Presentation transcript:

1 Chapter 2.7 Modula 2 Procedures

2 Summary Subroutines Procedures –Block structured languages –Scope rules –Memory Management Procedure Parameters –Value Parameters –Variable Parameters –Open Arrays Function Procedures Recursion

3 Summary Subroutines Procedures –Block structured languages –Scope rules –Memory Management Procedure Parameters –Value Parameters –Variable Parameters –Open Arrays Function Procedures Recursion

4 Subroutines CCC DoCCC Subroutine DoCCC End DoCCC

5 The Return Address The problem : During execution of a subroutine the control unit has to "remember" from where it was called (the return address) The solution : When a subroutine is called, the address of the next instruction is written in data memory. At the end of the subroutine this address is fetched and execution resumes at that location

6 Summary Subroutines Procedures –Block structured languages –Scope rules –Memory Management Procedure Parameters –Value Parameters –Variable Parameters –Open Arrays Function Procedures Recursion

7 PROCEDURES and MODULES MODULE Module Identifier ; Import/export lists BLOCK Module Identifier. PROCEDURE Procedure Identifier ; Parameters list BLOCK Procedure Identifier ;

8 Block BEGIN Constant Declarations Statement ; Type Declarations Variable Declarations END Procedure Declarations Local Module Declarations

9 Block Structured Languages A program is a set of nested blocks BEGIN... END BLUE. BEGIN... END PURPLE; BEGIN... END RED; BEGIN... END GREEN;

10 Summary Subroutines Procedures –Block structured languages –Scope rules –Memory Management Procedure Parameters –Value Parameters –Variable Parameters –Open Arrays Function Procedures Recursion

11 Block Structured Languages An identifier can be used everywhere within the block where it is declared VAR A: CARDINAL; VAR B: REAL; VAR C: CHAR; BEGIN... A:=...; B:=...; C:=...;... END BLUE. BEGIN... A:=...; B:=...; C:=...;... END PURPLE; BEGIN... A:=...; B:=...; C:=...;... END RED; BEGIN... A:=...; B:=...; C:=...;... END GREEN; XX X X

12 Block Structured Languages A global identifier can be redefined The local definition dominates VAR A: CARDINAL; VAR B: REAL; VAR A,C: CHAR; BEGIN... A:= 3;... END BLUE. BEGIN... A:= 2; B:=...; END PURPLE; BEGIN... A:="$"; B:=...; C:=...;... END RED; BEGIN... A:= 1; B:=...;... END GREEN;

13 Computing the GCD Top level design : Read value of x and y Compute g, the GCD of x and y Write the value of g MODULE GCD1;... VAR x,y,g : CARDINAL; BEGIN (* Read value of x and y *)... (* Compute g, the GCD of x and y *)... (* Write value of g *)... END GCD1.

14 Computing the GCD with global variables BEGIN... END AskNumbers; BEGIN... END ComputeGCD; BEGIN … END PrintGCD; VAR x,y,g : CARDINAL; BEGIN AskNumbers; ComputeGCD; PrintGCD END GCD. PROCEDURE AskNumbers; PROCEDURE ComputeGCD; PROCEDURE PrintGCD MODULE GCD;

15 Computing the GCD Main MODULE MODULE GCD3;... VAR x,y,g : CARDINAL; PROCEDURE AskNumbers;... END AskNumbers; PROCEDURE ComputeGCD;... END ComputeGCD; PROCEDURE PrintGCD;... END PrintGCD; BEGIN AskNumbers; ComputeGCD; PrintGCD; END GCD3.

16 Computing the GCD PROCEDURE AskNumbers PROCEDURE AskNumbers; BEGIN (* Read value of global variables x and y *) WriteString("Enter a cardinal number please "); ReadCard(x); WriteString("Enter a cardinal number please "); ReadCard(y); END AskNumbers;

17 Computing the GCD PROCEDURE ComputeGCD PROCEDURE ComputeGCD; (* Computes g, the GCD of x and y *) (* x,y and g are global CARDINAL variables *) BEGIN WHILE x # y DO IF x > y THEN x := x - y ELSE y := y - x END (* IF *) END; (* WHILE *) g := x; END ComputeGCD;

18 Computing the GCD PROCEDURE PrintGCD PROCEDURE PrintGCD; (* Write value of global variable g *) BEGIN WriteString(" The GCD of these numbers is : "); WriteCard(g,10); WriteLn END PrintGCD;

19 Summary Subroutines Procedures –Block structured languages –Scope rules –Memory Management Procedure Parameters –Value Parameters –Variable Parameters –Open Arrays Function Procedures Recursion

20 Scope of Procedure Calls BEGIN…;YELLOW; PURPLE;... END BLUE. MODULE BLUE BEGIN …;YELLOW; PURPLE; RED; GREEN;... END PURPLE; BEGIN …YELLOW; PURPLE; RED; GREEN;... END RED; BEGIN... YELLOW; PURPLE; RED; GREEN;... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN …;YELLOW; ORANGE;PURPLE;... END YELLOW; BEGIN …;YELLOW; ORANGE;PURPLE;... END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE;

21 Data Memory Usage (1) BEGIN…;YELLOW; PURPLE;... END BLUE. MODULE BLUE BEGIN … RED; GREEN;... END PURPLE; BEGIN …END RED; BEGIN... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : BLUE Activation Record

22 Data Memory Usage (2) BEGIN…;YELLOW; PURPLE;... END BLUE. MODULE BLUE BEGIN … RED; GREEN;... END PURPLE; BEGIN …END RED; BEGIN... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : YELLOW BLUE Activation Records Return Address

23 Data Memory Usage (3) BEGIN…;YELLOW; PURPLE;... END BLUE. MODULE BLUE BEGIN … RED; GREEN;... END PURPLE; BEGIN …END RED; BEGIN... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : YELLOW ORANGE BLUE Activation Records

24 Data Memory Usage (4) BEGIN…;YELLOW; PURPLE;... END BLUE. MODULE BLUE BEGIN … RED; GREEN;... END PURPLE; BEGIN …END RED; BEGIN... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : YELLOW BLUE Activation Records

25 Data Memory Usage (5) BEGIN…;YELLOW; PURPLE;... END BLUE. MODULE BLUE BEGIN … RED; GREEN;... END PURPLE; BEGIN …END RED; BEGIN... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : BLUE Activation Records

26 Data Memory Usage (6) BEGIN…;YELLOW; PURPLE;... END BLUE. MODULE BLUE BEGIN … RED; GREEN;... END PURPLE; BEGIN …END RED; BEGIN... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : PURPLE BLUE Activation Records

27 Data Memory Usage (7) BEGIN…;YELLOW; PURPLE;... END BLUE. MODULE BLUE BEGIN … RED; GREEN;... END PURPLE; BEGIN …END RED; BEGIN... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : PURPLE BLUE RED Activation Records

28 Data Memory Usage (8) BEGIN…;YELLOW; PURPLE;... END BLUE. MODULE BLUE BEGIN … RED; GREEN;... END PURPLE; BEGIN …END RED; BEGIN... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : PURPLE BLUE Activation Records

29 Data Memory Usage (9) BEGIN…;YELLOW; PURPLE;... END BLUE. MODULE BLUE BEGIN … RED; GREEN;... END PURPLE; BEGIN …END RED; BEGIN... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : PURPLE BLUE GREEN Activation Records

30 Data Memory Usage (10) BEGIN…;YELLOW; PURPLE;... END BLUE. MODULE BLUE BEGIN … RED; GREEN;... END PURPLE; BEGIN …END RED; BEGIN... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : PURPLE BLUE Activation Records

31 Data Memory Usage (11) BEGIN…;YELLOW; PURPLE;... END BLUE. MODULE BLUE BEGIN … RED; GREEN;... END PURPLE; BEGIN …END RED; BEGIN... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : BLUE Activation Record

32 Dynamic Links Base Register Data Memory BLUE Activation Record YELLOW Activation Record ORANGE Activation Record

33 Data Accessibility (1) BEGIN…; PURPLE;... END BLUE. MODULE BLUE BEGIN … GREEN;... END PURPLE; BEGIN …END RED; BEGIN …YELLOW;... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : BLUE Local : BLUE Global 1 : Global 2 :

34 Data Accessibility (2) BEGIN…; PURPLE;... END BLUE. MODULE BLUE BEGIN … GREEN;... END PURPLE; BEGIN …END RED; BEGIN …YELLOW;... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : BLUE PURPLE Local : PURPLE Global 1 : BLUE Global 2 :

35 Data Accessibility (3) BEGIN…; PURPLE;... END BLUE. MODULE BLUE BEGIN … GREEN;... END PURPLE; BEGIN …END RED; BEGIN …YELLOW;... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : BLUE PURPLE GREEN Local : GREEN Global 1 : PURPLE Global 2 : BLUE

36 Data Accessibility (4) BEGIN…; PURPLE;... END BLUE. MODULE BLUE BEGIN … GREEN;... END PURPLE; BEGIN …END RED; BEGIN …YELLOW;... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : BLUE PURPLE GREEN YELLOW Local : YELLOW Global 1 : BLUE Global 2 :

37 Data Accessibility (5) BEGIN…; PURPLE;... END BLUE. MODULE BLUE BEGIN … GREEN;... END PURPLE; BEGIN …END RED; BEGIN …YELLOW;... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : BLUE PURPLE GREEN YELLOW ORANGE Local : ORANGE Global 1 : YELLOW Global 2 : BLUE

38 Data Accessibility (6) BEGIN…; PURPLE;... END BLUE. MODULE BLUE BEGIN … GREEN;... END PURPLE; BEGIN …END RED; BEGIN …YELLOW;... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : BLUE PURPLE GREEN YELLOW Local : YELLOW Global 1 : BLUE Global 2 :

39 Data Accessibility (7) BEGIN…; PURPLE;... END BLUE. MODULE BLUE BEGIN … GREEN;... END PURPLE; BEGIN …END RED; BEGIN …YELLOW;... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : BLUE PURPLE GREEN Local : GREEN Global 1 : PURPLE Global 2 : BLUE

40 Data Accessibility (8) BEGIN…; PURPLE;... END BLUE. MODULE BLUE BEGIN … GREEN;... END PURPLE; BEGIN …END RED; BEGIN …YELLOW;... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : BLUE PURPLE Local : PURPLE Global 1 : BLUE Global 2 :

41 Data Accessibility (9) BEGIN…; PURPLE;... END BLUE. MODULE BLUE BEGIN … GREEN;... END PURPLE; BEGIN …END RED; BEGIN …YELLOW;... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : BLUE Local : BLUE Global 1 : Global 2 :

42 Static Links Base Register Static links Data Memory BLUE Activation Record PURPLE Activation Record GREEN Activation Record YELLOW Activation Record ORANGE Activation Record Dynamic links

43 Data Accessibility (1) BEGIN…; PURPLE;... END BLUE. MODULE BLUE BEGIN … GREEN;... END PURPLE; BEGIN …END RED; BEGIN …YELLOW;... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : BLUE Local : BLUE Global 1 : Global 2 :

44 Data Accessibility (2) BEGIN…; PURPLE;... END BLUE. MODULE BLUE BEGIN … GREEN;... END PURPLE; BEGIN …END RED; BEGIN …YELLOW;... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : BLUE PURPLE Local : PURPLE Global 1 : BLUE Global 2 : BLUE

45 Data Accessibility (3) BEGIN…; PURPLE;... END BLUE. MODULE BLUE BEGIN … GREEN;... END PURPLE; BEGIN …END RED; BEGIN …YELLOW;... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : BLUE PURPLE GREEN Local : GREEN Global 1 : PURPLE Global 2 : BLUE

46 Data Accessibility (4) BEGIN…; PURPLE;... END BLUE. MODULE BLUE BEGIN … GREEN;... END PURPLE; BEGIN …END RED; BEGIN …YELLOW;... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : BLUE PURPLE GREEN YELLOW Local : YELLOW Global 1 : BLUE Global 2 :

47 Data Accessibility (5) BEGIN…; PURPLE;... END BLUE. MODULE BLUE BEGIN … GREEN;... END PURPLE; BEGIN …END RED; BEGIN …YELLOW;... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : BLUE PURPLE GREEN YELLOW ORANGE Local : ORANGE Global 1 : YELLOW Global 2 : BLUE

48 Data Accessibility (6) BEGIN…; PURPLE;... END BLUE. MODULE BLUE BEGIN … GREEN;... END PURPLE; BEGIN …END RED; BEGIN …YELLOW;... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : BLUE PURPLE GREEN YELLOW Local : YELLOW Global 1 : BLUE Global 2 :

49 Data Accessibility (7) BEGIN…; PURPLE;... END BLUE. MODULE BLUE BEGIN … GREEN;... END PURPLE; BEGIN …END RED; BEGIN …YELLOW;... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : BLUE PURPLE GREEN Local : GREEN Global 1 : PURPLE Global 2 : BLUE

50 Data Accessibility (8) BEGIN…; PURPLE;... END BLUE. MODULE BLUE BEGIN … GREEN;... END PURPLE; BEGIN …END RED; BEGIN …YELLOW;... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : BLUE PURPLE Local : PURPLE Global 1 : BLUE Global 2 :

51 Data Accessibility (8) BEGIN…; PURPLE;... END BLUE. MODULE BLUE BEGIN … GREEN;... END PURPLE; BEGIN …END RED; BEGIN …YELLOW;... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN … ORANGE... END YELLOW; BEGIN … END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE; Data Memory : BLUE Local : BLUE Global 1 : Global 2 :

52 Summary Subroutines Procedures –Block structured languages –Scope rules –Memory Management Procedure Parameters –Value Parameters –Variable Parameters –Open Arrays Function Procedures Recursion

53 Procedure Parameters CCC abc CCC uvw CCC xyz DoCCC(abc) DoCCC(uvw) Subroutine DoCCC(xyz) End DoCCC Actual Parameters Formal Parameters

54 Procedure Parameters Formal parameters are declared in the procedure. Actual parameters appear in the procedure call. Formal and actual parameters MUST have STRICTLY the same type ( = a common type declaration - identical but distinct declarations are not good enough). Association between actual and formal parameters is positional: –PROCEDURE DoIt(a,b,c) DoIt(x,y,z) –a ~ x ; b ~ y; c ~ z;

55 Parameter Passing Base Register Data Memory Calling Block Activation Record Called Block Activation Record Static link Dynamic links Actual Parameters

56 Summary Subroutines Procedures –Block structured languages –Scope rules –Memory Management Procedure Parameters –Value Parameters –Variable Parameters –Open Arrays Function Procedures Recursion

57 Value Parameters The formal parameter is a local variable The value of the actual parameter is assigned to the formal parameter The actual parameter can be any expression, as long as its value belongs to the type of the formal parameter Assigning a new value to the formal parameter has no effect on the actual parameter. The formal parameter requires as much memory space as the actual parameter.

58 Value Parameters MODULE TestValPar (* Parameter passing by Value *); FROM InOutIMPORT Write,WriteLn; VARx: CHAR; PROCEDURE P (c:CHAR); BEGINWrite(c);c:='B';Write(c); RETURN END P; BEGIN x:='A';P(x);Write(x);WriteLn END TestValPar. ABAABA

59 Summary Subroutines Procedures –Block structured languages –Scope rules –Memory Management Procedure Parameters –Value Parameters –Variable Parameters –Open Arrays Function Procedures Recursion

60 Variable Parameters (also called “parameter passing by address”) The formal parameter is the address of a variable. The address of the actual parameter is assigned to the formal parameter The actual parameter must be a variable Assigning a new value to the formal parameter assigns this value to the actual parameter. The size of the formal parameter in memory is the size of an address (usually 4 bytes)

61 Variable Parameters MODULE TestValPar (* Parameter passing by Variable *); FROM InOutIMPORT Write,WriteLn; VARx: CHAR; PROCEDURE P (VAR c:CHAR); BEGINWrite(c);c:='B';Write(c); RETURN END P; BEGIN x:='A';P(x);Write(x);WriteLn END TestValPar. ABBABB

62 Computing the GCD Main MODULE, with parameters MODULE GCD4;... VAR x,y,g : CARDINAL; PROCEDURE AskNumber();... END AskNumbers; PROCEDURE ComputeGCD();... END ComputeGCD; PROCEDURE PrintGCD();... END PrintGCD; BEGIN AskNumber(x); AskNumber(y); ComputeGCD(x,y,g); PrintGCD(x,y,g); END GCD4.

63 Computing the GCD PROCEDURE AskNumber PROCEDURE AskNumber( VAR n:CARDINAL); BEGIN (* Reads value of a variable parameter n *) WriteString("Enter a cardinal number please "); ReadCard(n); END AskNumber;

64 Computing the GCD PROCEDURE ComputeGCD PROCEDURE ComputeGCD(x,y:CARDINAL; VAR g:CARDINAL); (* Computes g, the GCD of x and y *) BEGIN WHILE x # y DO IF x > y THEN x := x - y ELSE y := y - x END (* IF *) END; (* WHILE *) g := x; END ComputeGCD;

65 Computing the GCD PROCEDURE PrintGCD PROCEDURE PrintGCD(x,y,g:CARDINAL); (* Write value of parameters x,y and g *) BEGIN WriteString(" The GCD of "); WriteCard(x,1); WriteString(" and "); WriteCard(y,1); WriteString(" is : "); WriteCard(g,1); WriteLn END PrintGCD;

66 Summary Subroutines Procedures –Block structured languages –Scope rules –Memory Management Procedure Parameters –Value Parameters –Variable Parameters –Open Arrays Function Procedures Recursion

67 Open Arrays Strong typing causes absurd complexity: TYPE Table20 = ARRAY[1..20] OF REAL; Table30 = ARRAY[1..30] OF REAL;... PROCEDURE Sort20(VAR a:Table20);... END Sort20; PROCEDURE Sort30(VAR a:Table30);... END Sort30;

68 Open Arrays with one-dimensional arrays used as formal parameters, the index type can be omitted VAR Tab20 = ARRAY[1..20] OF REAL; Tab30 = ARRAY[1..30] OF REAL;... PROCEDURE Sort(VAR a:ARRAY OF REAL);... END Sort;... Sort(Tab20);... Sort(Tab30);...

69 The function HIGH PROCEDURE Sort(VAR a:ARRAY OF...);... last := HIGH(a);... END Sort; HIGH(A) = ORD(index of last element of array a) x:ARRAY[0..20]OF... Sort(x) HIGH(a) = 20 y:ARRAY[1..30]OF... Sort(y) HIGH(a) = 29 z:ARRAY[mon..sun]OF... Sort(z) HIGH(a) = 6

70 Summary Subroutines Procedures –Block structured languages –Scope rules –Memory Management Procedure Parameters –Value Parameters –Variable Parameters –Open Arrays Function Procedures Recursion

71 Function Procedures for computing a simple value function return statement

72 Value Returned by Function Base Register Data Memory Calling Block Activation Record Called Block Activation Record Static link Dynamic links Actual Parameters Returned Value

73 Computing the GCD Main MODULE, with parameters MODULE GCD4;... VAR x,y,g : CARDINAL; PROCEDURE AskNumber();... END AskNumbers; PROCEDURE ComputeGCD();... END ComputeGCD; PROCEDURE PrintGCD();... END PrintGCD; BEGIN AskNumber(x); AskNumber(y); ComputeGCD(x,y,g); PrintGCD(x,y,g); END GCD4.

74 Computing the GCD Main MODULE, with function procedures MODULE GCD5;... VAR x,y,g : CARDINAL; PROCEDURE AskNumber():CARDINAL;...; PROCEDURE ComputeGCD():CARDINAL;...; PROCEDURE PrintGCD();... END PrintGCD; BEGIN x := AskNumber(); y := AskNumber(); g := ComputeGCD(x,y); PrintGCD(x,y,g); END GCD5.

75 Computing the GCD Main MODULE, with function procedures MODULE GCD6;... VAR x,y : CARDINAL; PROCEDURE AskNumber():CARDINAL;...; PROCEDURE ComputeGCD():CARDINAL;...; PROCEDURE PrintGCD();... END PrintGCD; BEGIN x := AskNumber(); y := AskNumber(); PrintGCD(x,y, ComputeGCD(x,y)); END GCD6.

76 Computing the GCD Function PROCEDURE AskNumber PROCEDURE AskNumber():CARDINAL; VAR n : CARDINAL; BEGIN (* Reads value of a variable parameter n *) WriteString("Enter a cardinal number please "); ReadCard(n); RETURN n END AskNumber;

77 Computing the GCD PROCEDURE ComputeGCD PROCEDURE ComputeGCD(x,y:CARDINAL):CARDINAL; (* Computes the GCD of x and y *) BEGIN WHILE x # y DO IF x > y THEN x := x - y ELSE y := y - x END (* IF *) END; (* WHILE *) RETURN x; END ComputeGCD;

78 Computing the GCD PROCEDURE PrintGCD PROCEDURE PrintGCD(x,y,g:CARDINAL); (* Write value of parameters x,y and g *) BEGIN WriteString(" The GCD of "); WriteCard(x,1); WriteString(" and "); WriteCard(y,1); WriteString(" is : "); WriteCard(g,1); WriteLn END PrintGCD;

79 Side Effects Functions are intended for computing a value, and nothing else! Functions can modify global variables and VAR parameters. Such modifications are "Side Effects" Side effects should be avoided. Some functions also perform Input/Output operations. Such side effects are harmless.

80 Computing the GCD PROCEDURE ComputeGCD, With side effect PROCEDURE ComputeGCD(VAR x,y:CARDINAL):CARDINAL; (* Passing x and y by VARiable causes a side effect *) (* Computes the GCD of x and y *) BEGIN WHILE x # y DO IF x > y THEN x := x - y ELSE y := y - x END (* IF *) END; (* WHILE *) RETURN x; END ComputeGCD;

81 Computing the GCD Function PROCEDURE AskNumber PROCEDURE AskNumber():CARDINAL; BEGIN (* Reads value of a variable parameter n *) (* The following statement can be considered as a harmless side effect *) WriteString("Enter a cardinal number please "); ReadCard(n); RETURN n END AskNumber;

82 Summary Subroutines Procedures –Block structured languages –Scope rules –Memory Management Procedure Parameters –Value Parameters –Variable Parameters –Open Arrays Function Procedures Recursion

83 Block Structured MODULE BEGIN…;YELLOW; PURPLE;... END BLUE. MODULE BLUE BEGIN …;YELLOW; PURPLE; RED; GREEN;... END PURPLE; BEGIN …YELLOW; PURPLE; RED; GREEN;... END RED; BEGIN... YELLOW; PURPLE; RED; GREEN;... END GREEN; PROCEDURE PURPLE; PROCEDURE RED; PROCEDURE GREEN; BEGIN …;YELLOW; ORANGE;PURPLE;... END YELLOW; BEGIN …;YELLOW; ORANGE;PURPLE;... END ORANGE; PROCEDURE YELLOW; PROCEDURE ORANGE;

84 Hide in Doll (1)

85 Hide in Doll (2)... PROCEDURE Hide; BEGIN OpenDoll; IF AnotherDoll THEN Hide ELSE PutSweet END; CloseDoll END Hide; BEGIN Hide END HideInDoll.

86 Hide in Doll (3)

87 Hide in Doll (4) PROCEDURE Hide; BEGIN OpenDoll; IF AnotherDoll THEN Hide ELSE PutSweet END; CloseDoll END Hide; BEGIN Hide END HideInDoll.

88 Hide in Doll (5)

89 Hide in Doll (6) PROCEDURE Hide; BEGIN OpenDoll; IF AnotherDoll THEN Hide ELSE PutSweet END; CloseDoll END Hide; BEGIN Hide END HideInDoll. PROCEDURE Hide; BEGIN OpenDoll; IF AnotherDoll THEN Hide ELSE PutSweet END; CloseDoll END Hide;

90 Hide in Doll (7)

91 Hide in Doll (8) PROCEDURE Hide; BEGIN OpenDoll; IF AnotherDoll THEN Hide ELSE PutSweet END; CloseDoll END Hide; BEGIN Hide END HideInDoll. PROCEDURE Hide; BEGIN OpenDoll; IF AnotherDoll THEN Hide ELSE PutSweet END; CloseDoll END Hide; PROCEDURE Hide; BEGIN OpenDoll; IF AnotherDoll THEN Hide ELSE PutSweet END; CloseDoll END Hide;

92 Hide in Doll (9)

93 Hide in Doll (10) PROCEDURE Hide; BEGIN OpenDoll; IF AnotherDoll THEN Hide ELSE PutSweet END; CloseDoll END Hide; BEGIN Hide END HideInDoll. PROCEDURE Hide; BEGIN OpenDoll; IF AnotherDoll THEN Hide ELSE PutSweet END; CloseDoll END Hide; PROCEDURE Hide; BEGIN OpenDoll; IF AnotherDoll THEN Hide ELSE PutSweet END; CloseDoll END Hide;

94 Hide in Doll (11)

95 Hide in Doll (12) PROCEDURE Hide; BEGIN OpenDoll; IF AnotherDoll THEN Hide ELSE PutSweet END; CloseDoll END Hide; BEGIN Hide END HideInDoll. PROCEDURE Hide; BEGIN OpenDoll; IF AnotherDoll THEN Hide ELSE PutSweet END; CloseDoll END Hide; PROCEDURE Hide; BEGIN OpenDoll; IF AnotherDoll THEN Hide ELSE PutSweet END; CloseDoll END Hide;

96 Hide in Doll (13)

97 Hide in Doll (14) PROCEDURE Hide; BEGIN OpenDoll; IF AnotherDoll THEN Hide ELSE PutSweet END; CloseDoll END Hide; BEGIN Hide END HideInDoll. PROCEDURE Hide; BEGIN OpenDoll; IF AnotherDoll THEN Hide ELSE PutSweet END; CloseDoll END Hide; PROCEDURE Hide; BEGIN OpenDoll; IF AnotherDoll THEN Hide ELSE PutSweet END; CloseDoll END Hide;

98 Hide in Doll (15)

99 Hide in Doll (16) PROCEDURE Hide; BEGIN OpenDoll; IF AnotherDoll THEN Hide ELSE PutSweet END; CloseDoll END Hide; BEGIN Hide END HideInDoll. PROCEDURE Hide; BEGIN OpenDoll; IF AnotherDoll THEN Hide ELSE PutSweet END; CloseDoll END Hide; PROCEDURE Hide; BEGIN OpenDoll; IF AnotherDoll THEN Hide ELSE PutSweet END; CloseDoll END Hide;

100 Hide in Doll (17)

101 Hide in Doll (18) PROCEDURE Hide; BEGIN OpenDoll; IF AnotherDoll THEN Hide ELSE PutSweet END; CloseDoll END Hide; BEGIN Hide END HideInDoll. PROCEDURE Hide; BEGIN OpenDoll; IF AnotherDoll THEN Hide ELSE PutSweet END; CloseDoll END Hide; PROCEDURE Hide; BEGIN OpenDoll; IF AnotherDoll THEN Hide ELSE PutSweet END; CloseDoll END Hide;

102 Recursive Procedures (1) Data Memory : MODULE Recursion; FROM InOut IMPORT WriteCard, WriteLn; VAR x : CARDINAL; PROCEDURE P(v:CARDINAL); BEGIN WriteCard(v,1); IF v # 0 THEN v:=v-1; P(v) END; WriteCard(v,1); END P; BEGIN P(3); WriteLn END Recursion.

103 Data Memory : v = 3 MODULE Recursion; FROM InOut IMPORT WriteCard, WriteLn; VAR x : CARDINAL; PROCEDURE P(v:CARDINAL); BEGIN WriteCard(v,1); IF v # 0 THEN v:=v-1; P(v) END; WriteCard(v,1); END P; BEGIN P(3); WriteLn END Recursion. 3 Recursive Procedures (2)

104 Data Memory : v = 2 MODULE Recursion; FROM InOut IMPORT WriteCard, WriteLn; VAR x : CARDINAL; PROCEDURE P(v:CARDINAL); BEGIN WriteCard(v,1); IF v # 0 THEN v:=v-1; P(v) END; WriteCard(v,1); END P; BEGIN P(3); WriteLn END Recursion. 32 Recursive Procedures (3)

105 Recursive Procedures (4) Data Memory : v = 2 v = 1 MODULE Recursion; FROM InOut IMPORT WriteCard, WriteLn; VAR x : CARDINAL; PROCEDURE P(v:CARDINAL); BEGIN WriteCard(v,1); IF v # 0 THEN v:=v-1; P(v) END; WriteCard(v,1); END P; BEGIN P(3); WriteLn END Recursion. 321

106 Recursive Procedures (5) Data Memory : v = 2 v = 1 v = 0 MODULE Recursion; FROM InOut IMPORT WriteCard, WriteLn; VAR x : CARDINAL; PROCEDURE P(v:CARDINAL); BEGIN WriteCard(v,1); IF v # 0 THEN v:=v-1; P(v) END; WriteCard(v,1); END P; BEGIN P(3); WriteLn END Recursion. v =

107 Data Memory : v = 2 v = 1 v = 0 MODULE Recursion; FROM InOut IMPORT WriteCard, WriteLn; VAR x : CARDINAL; PROCEDURE P(v:CARDINAL); BEGIN WriteCard(v,1); IF v # 0 THEN v:=v-1; P(v) END; WriteCard(v,1); END P; BEGIN P(3); WriteLn END Recursion. v = Recursive Procedures (6)

108 Data Memory : v = 2 v = 1 v = 0 MODULE Recursion; FROM InOut IMPORT WriteCard, WriteLn; VAR x : CARDINAL; PROCEDURE P(v:CARDINAL); BEGIN WriteCard(v,1); IF v # 0 THEN v:=v-1; P(v) END; WriteCard(v,1); END P; BEGIN P(3); WriteLn END Recursion Recursive Procedures (7)

109 Data Memory : v = 2 v = 1 v = 0 MODULE Recursion; FROM InOut IMPORT WriteCard, WriteLn; VAR x : CARDINAL; PROCEDURE P(v:CARDINAL); BEGIN WriteCard(v,1); IF v # 0 THEN v:=v-1; P(v) END; WriteCard(v,1); END P; BEGIN P(3); WriteLn END Recursion Recursive Procedures (8)

110 Data Memory : v = 2 v = 1 MODULE Recursion; FROM InOut IMPORT WriteCard, WriteLn; VAR x : CARDINAL; PROCEDURE P(v:CARDINAL); BEGIN WriteCard(v,1); IF v # 0 THEN v:=v-1; P(v) END; WriteCard(v,1); END P; BEGIN P(3); WriteLn END Recursion Recursive Procedures (9)

111 Data Memory : v = 2 v = 1 MODULE Recursion; FROM InOut IMPORT WriteCard, WriteLn; VAR x : CARDINAL; PROCEDURE P(v:CARDINAL); BEGIN WriteCard(v,1); IF v # 0 THEN v:=v-1; P(v) END; WriteCard(v,1); END P; BEGIN P(3); WriteLn END Recursion Recursive Procedures (10)

112 Data Memory : v = 2 MODULE Recursion; FROM InOut IMPORT WriteCard, WriteLn; VAR x : CARDINAL; PROCEDURE P(v:CARDINAL); BEGIN WriteCard(v,1); IF v # 0 THEN v:=v-1; P(v) END; WriteCard(v,1); END P; BEGIN P(3); WriteLn END Recursion Recursive Procedures (11)

113 Data Memory : v = 2 MODULE Recursion; FROM InOut IMPORT WriteCard, WriteLn; VAR x : CARDINAL; PROCEDURE P(v:CARDINAL); BEGIN WriteCard(v,1); IF v # 0 THEN v:=v-1; P(v) END; WriteCard(v,1); END P; BEGIN P(3); WriteLn END Recursion Recursive Procedures (12)

114 Data Memory : MODULE Recursion; FROM InOut IMPORT WriteCard, WriteLn; VAR x : CARDINAL; PROCEDURE P(v:CARDINAL); BEGIN WriteCard(v,1); IF v # 0 THEN v:=v-1; P(v) END; WriteCard(v,1); END P; BEGIN P(3); WriteLn END Recursion Recursive Procedures (13)

115 Data Memory : MODULE Recursion; FROM InOut IMPORT WriteCard, WriteLn; VAR x : CARDINAL; PROCEDURE P(VAR v:CARDINAL); BEGIN WriteCard(v,1); IF v # 0 THEN v:=v-1; P(v) END; WriteCard(v,1); END P; BEGIN x:=3;P(x); WriteLn END Recursion. Recursive Procedures (13)

116 Recursive Algorithms Factorial Fac(n) = 1*2*3*4*…*(n-1)*n Recursive definition : Fac(n) = (n)*Fac(n-1) Trivial solution : Fac(1) = 1 Recursive procedure : PROCEDURE Fac(n:CARDINAL): CARDINAL; BEGIN IF n > 1 THEN RETURN n*Fac(n-1) ELSE RETURN 1 END (* IF *) END Fac;

117 Binary Search # elements > 1 Binary Search No Yes # keys > 1 No Yes Key > Key middle No Yes Key = Only Key Binary Search left half Binary Search right half Not Found Find a given key in a sorted array of keys

118 Quicksort Partition array A into two parts A1 and A2 in such a way that all keys in A1 are Pivot (Pivot has a value close to the median of key values) More than 1 element in A1 ? Apply QuickSort to array A1 More than 1 element in A2 ? Apply QuickSort to array A2 Yes

119 Towers of Hanoi ABC

120 ABC

121 ABC

122 ABC

123 Towers of Hanoi Global Organization MODULE Hanoi;... CONST Rings = 4; PROCEDURE MoveTower (Height:CARDINAL;From,Towards,Using:CHAR); PROCEDURE MoveDisk(TakeOff,PutOn:CHAR);.... END MoveDisk; BEGIN.... END MoveTower; BEGIN MoveTower(Rings,'A','C','B') END Hanoi.

124 MoveTower PROCEDURE MoveTower (Height:CARDINAL;From,Towards,Using:CHAR); PROCEDURE MoveDisk(TakeOff,PutOn:CHAR); BEGIN WriteString("Move one ring from "); WriteChar(TakeOff); WriteString(" to "); WriteChar(PutOn); WriteLn END MoveDisk; BEGIN IF Height > 0 THEN MoveTower(Height-1,From,Using,Towards); MoveDisk(From,Towards); MoveTower(Height-1,Using,Towards,From) END END MoveTower;


Download ppt "Chapter 2.7 Modula 2 Procedures. Summary Subroutines Procedures –Block structured languages –Scope rules –Memory Management Procedure Parameters –Value."

Similar presentations


Ads by Google