Subprogram ธนวัฒน์ แซ่ เอียบ
Subprograms as computational abstractions
Basic notions
General view of a subprogram
A subprogram´s data
Functions and procedures
Example of a function
General form of a function definition
Formal parameters of functions
Actual parameters and function calls
Calling a function in a main program
General form of a main program
Rewriting the factorial function into a procedure
General form of a procedure definition
Generalizing parameter passing
Models of Parameter Passing
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)
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
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
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
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);
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); ให้เขียนความสัมพันธ์ระหว่างตัวแปรทั้งหมดทุก ขั้นตอนที่เกิดขึ้น
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); ให้เขียนความสัมพันธ์ระหว่างตัวแปรทั้งหมดทุกขั้นตอนที่เกิดขึ้น
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); ให้เขียนความสัมพันธ์ระหว่างตัวแปรทั้งหมดทุกขั้นตอนที่เกิดขึ้น
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
A procedure with in/out parameters
Handling of in/out parameters
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
Subprogram calls: control flow
Execution trace
Call stack
ที่มา Bernhard Westfechtel : RWTH Aachen University Concepts of programming languages : Sebesta, Robert W.