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

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Introduction to C Programming
Symbol Table.
1) Scope a] Ada scope rules b] C scope rules 2) Parameter passing a] Ada parameter modes b) Parameter passing mechanisms COMP205 IMPERATIVE LANGUAGES 13.
Chapter 6 Type Checking. The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Programming Languages and Paradigms
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Chapter 2.5 Modula 2 Control Instructions. Modula 2 Control Statements Selection statements –BOOLEAN Selector : IF statement –Ordinal Selector : CASE.
Chapter 5 ( ) of Programming Languages by Ravi Sethi
Chapter 7: User-Defined Functions II
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Factorial Recursion stack Binary Search Towers of Hanoi
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
Run time vs. Compile time
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Chapter 9: Subprogram Control
James Tam Pointers In this section of notes you will learn about another type of variable that stores addresses rather than data.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
Chapter 8 Arrays and Strings
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CSC321: Programming Languages1 Programming Languages Tucker and Noonan Chapter 9: Functions 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 9 Functions It is better to have 100 functions.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Chapter 2.3 Modula 2 An Introduction. Summary First example Terminal symbols Comments Data –Constants –Variables Actions –Assignment statement –Control.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
CS 2104 Prog. Lang. Concepts Subprograms
Runtime Environments Compiler Construction Chapter 7.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
Compiler Construction
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
J.Tiberghien Chapter 2.10 Recursive Algorithms and Backtracking.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Programming Languages and Paradigms Imperative Programming.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
5.3 EVALUATION OF POSTFIX EXPRESSION For example, consider the evaluation of the following postfix expression using stacks: abc+d*f/- where, a=6, b=3,
Constructs for Data Organization and Program Control, Scope, Binding, and Parameter Passing. Expression Evaluation.
Procedure Activations Programming Language. Exploration Name ocurrenceDeclarationLocationValue scopeactivationstate a.From names to their declarations.
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
CSE / ENGR 142 Programming I
Run-Time Environments Chapter 7
Functions.
Implementing Subprograms
Principles of programming languages 4: Parameter passing, Scope rules
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Run-Time Environments
Object-Oriented Programming Using C++ Second Edition
Run-Time Environments
Presentation transcript:

Chapter 2.7 Modula 2 Procedures

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

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

Subroutines CCC DoCCC Subroutine DoCCC End DoCCC

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

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

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

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

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

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

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

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;

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.

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;

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.

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;

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;

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;

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

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;

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

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

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

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

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

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

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

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

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

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

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

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

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 :

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 :

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

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 :

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

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 :

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

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 :

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 :

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

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 :

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

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

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 :

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

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 :

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

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 :

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 :

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

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

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;

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

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

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.

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

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

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)

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

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.

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;

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;

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;

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

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;

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);...

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

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

Function Procedures for computing a simple value function return statement

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

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.

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.

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.

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;

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;

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;

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.

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;

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;

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

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;

Hide in Doll (1)

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

Hide in Doll (3)

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

Hide in Doll (5)

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;

Hide in Doll (7)

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;

Hide in Doll (9)

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;

Hide in Doll (11)

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;

Hide in Doll (13)

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;

Hide in Doll (15)

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;

Hide in Doll (17)

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;

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.

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)

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)

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

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 =

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)

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)

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)

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)

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)

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)

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)

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)

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)

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;

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

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

Towers of Hanoi ABC

ABC

ABC

ABC

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.

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;