Presentation is loading. Please wait.

Presentation is loading. Please wait.

Subprogram ธนวัฒน์ แซ่ เอียบ. Subprograms as computational abstractions.

Similar presentations


Presentation on theme: "Subprogram ธนวัฒน์ แซ่ เอียบ. Subprograms as computational abstractions."— Presentation transcript:

1 Subprogram ธนวัฒน์ แซ่ เอียบ

2 Subprograms as computational abstractions

3 Basic notions

4 General view of a subprogram

5 A subprogram´s data

6 Functions and procedures

7 Example of a function

8 General form of a function definition

9 Formal parameters of functions

10 Actual parameters and function calls

11 Calling a function in a main program

12 General form of a main program

13 Rewriting the factorial function into a procedure

14 General form of a procedure definition

15 Generalizing parameter passing

16 Models of Parameter Passing

17 Parameter Passing Methods Implementation Models: 1. Pass-by-value (in mode) –Either by physical move or access path –Disadvantages of access path method: Must write-protect in the called subprogram Accesses cost more (indirect addressing) –Disadvantages of physical move: Requires more storage (duplicated space) Cost of the moves (if the parameter is large)

18 Parameter Passing Methods 2. Pass-by-result (out mode) –Local’s value is passed back to the caller –Physical move is usually used –Disadvantages: If value is passed, time and space In both cases, order dependence may be a problem e.g. procedure sub1(y: int, z: int);... sub1(x, x); –Value of x in the caller depends on order of assignments at the return

19 Parameter Passing Methods 3. Pass-by-value-result (inout mode) –Physical move, both ways –Also called pass-by-copy –Disadvantages: Those of pass-by-result Those of pass-by-value

20 Parameter Passing Methods 4. Pass-by-reference (inout mode) –Pass an access path –Also called pass-by-sharing –Advantage: passing process is efficient (no copying and no duplicated storage) –Disadvantages: Slower accesses

21 Parameter Passing Methods Pass-by-reference - disadvantages (cont) b. Allows aliasing: i. Actual parameter collisions: e.g. procedure sub1(a: int, b: int);... sub1(x, x);

22 Example of Parameter Passing Consider the following C function void swap1(int a, int b){ int temp = a; a = b; b = temp; } Suppose this function is called with swap1(c,d); ให้เขียนความสัมพันธ์ระหว่างตัวแปรทั้งหมดทุก ขั้นตอนที่เกิดขึ้น

23 Example of Parameter Passing Consider the following C function (pass by reference) void swap2(int *a, int *b){ int temp = *a; *a = *b; *b = temp; } swap2 can be called with swap2(&c, &d); ให้เขียนความสัมพันธ์ระหว่างตัวแปรทั้งหมดทุกขั้นตอนที่เกิดขึ้น

24 Example of Parameter Passing Consider the following C++ function (pass by reference) void swap3(int &a, int &b){ int temp = a; a = b; b = temp; } swap3 can be called with swap3(c, d); ให้เขียนความสัมพันธ์ระหว่างตัวแปรทั้งหมดทุกขั้นตอนที่เกิดขึ้น

25 Memory states during program execution 1. At program start 2. Immediately after entering the procedure 3. During the execution of the body 4. Immediately before leaving the procedure 5. Immediately after leaving the procedure

26 A procedure with in/out parameters

27 Handling of in/out parameters

28 Memory states during program execution

29 1. At program start 2. Immediately after entering the procedure 3. During the execution of the body 4. Immediately before leaving the procedure 5. Immediately after leaving the procedure

30 Subprogram calls: control flow

31 Execution trace

32 Call stack

33 ที่มา Bernhard Westfechtel : RWTH Aachen University Concepts of programming languages : Sebesta, Robert W.


Download ppt "Subprogram ธนวัฒน์ แซ่ เอียบ. Subprograms as computational abstractions."

Similar presentations


Ads by Google