Session 7 : Subprograms
Two fundamental abstraction facilities can be included in a programming language: process abstraction and data abstraction.
Fundamentals of Subprograms
general subprogram characteristic :
- Each subprogram has a single entry point
- The calling program is suspended during execution of the called subprogram
- Control always returns to the caller when the called subprogram’s execution terminates
basic definition :
A subprogram definition describes the interface to and the actions of the subprogram abstraction. A subprogram call is the explicit request that a specific subprogram be executed. A subprogram is said to be active if, after having been called, it has begun execution but has not yet completed that execution.
Semantics Models of Parameter Passing
there are three types of parameter passing :
- In mode
- Out mode
- In-out mode
Passing parameter methods
1. Pass-by-Value (In Mode)
The value of the actual parameter is used to initialize the corresponding formal parameter
- Normally implemented by copying
- Can be implemented by transmitting an access path but not recommended (enforcing write protection is not easy)
- Disadvantages (if by physical move): additional storage is required (stored twice) and the actual move can be costly (for large parameters)
- Disadvantages (if by access path method): must write-protect in the called subprogram and accesses cost more (indirect addressing)
2. Pass-by-Result (Out Mode)
* When a parameter is passed by result, no value is transmitted to the subprogram; the corresponding formal parameter acts as a local variable; its value is transmitted to caller’s actual parameter when control is returned to the caller, by physical move
- Require extra storage location and copy operation
* Potential problems:
- sub(p1, p1); whichever formal parameter is copied back will represent the current value of p1
- sub(list[sub], sub); Compute address of list[sub] at the beginning of the subprogram or end?
3. Pass-by-Value-Result (inout Mode)
* A combination of pass-by-value and pass-by-result
* Sometimes called pass-by-copy
* Formal parameters have local storage
* Disadvantages:
- Those of pass-by-result
- Those of pass-by-value
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 (compared to pass-by-value) to formal parameters
- Potentials for unwanted side effects (collisions)
- Unwanted aliases (access broadened)
fun(total, total); fun(list[i], list[j]; fun(list[i], i);
5. Pass-by-Name (Inout Mode)
- By textual substitution
- Formals are bound to an access method at the time of the call, but actual binding to a value or address takes place at the time of a reference or assignment
- Allows flexibility in late binding
- Implementation requires that the referencing environment of the caller is passed with the parameter, so the actual parameter address can be calculated
Implementing Parameter-Passing Methods
- In most languages parameter communication takes place thru the run-time stack
- Pass-by-reference are the simplest to implement; only an address is placed in the stack
Design Considerations for Parameter Passing
* Two important considerations
- Efficiency
- One-way or two-way data transfer
* But the above considerations are in conflict
- Good programming suggest limited access to variables, which means one-way whenever possible
- But pass-by-reference is more efficient to pass structures of significant size
Overloaded Subprograms
An overloaded subprogram is a subprogram that has the same name as another subprogram in the same referencing environment. C++, Java, Ada, and C# include predefined overloaded subprograms.
a closure is a subprogram and the referencing environment where it was defined. Closures are not useful, so such languages do not support them.
A coroutine is a special kind of subprogram. Rather than the master-slave relationship between a caller and a called subprogram that exists with conventional subprograms, caller and called coroutines are more equitable. In fact, the coroutine control mechanism is often called the symmetric unit control model. Coroutines can have multiple entry points, which are controlled by the coroutines themselves. They also have the means to maintain their status between activations. This means that coroutines must be history sensitive and thus have static local variables. Secondary executions of a coroutine often begin at points other than its beginning. Because of this, the invocation of a coroutine is called a resume rather than a call.
Fundamentals of Subprograms
general subprogram characteristic :
- Each subprogram has a single entry point
- The calling program is suspended during execution of the called subprogram
- Control always returns to the caller when the called subprogram’s execution terminates
basic definition :
A subprogram definition describes the interface to and the actions of the subprogram abstraction. A subprogram call is the explicit request that a specific subprogram be executed. A subprogram is said to be active if, after having been called, it has begun execution but has not yet completed that execution.
Semantics Models of Parameter Passing
there are three types of parameter passing :
- In mode
- Out mode
- In-out mode
Passing parameter methods
1. Pass-by-Value (In Mode)
The value of the actual parameter is used to initialize the corresponding formal parameter
- Normally implemented by copying
- Can be implemented by transmitting an access path but not recommended (enforcing write protection is not easy)
- Disadvantages (if by physical move): additional storage is required (stored twice) and the actual move can be costly (for large parameters)
- Disadvantages (if by access path method): must write-protect in the called subprogram and accesses cost more (indirect addressing)
2. Pass-by-Result (Out Mode)
* When a parameter is passed by result, no value is transmitted to the subprogram; the corresponding formal parameter acts as a local variable; its value is transmitted to caller’s actual parameter when control is returned to the caller, by physical move
- Require extra storage location and copy operation
* Potential problems:
- sub(p1, p1); whichever formal parameter is copied back will represent the current value of p1
- sub(list[sub], sub); Compute address of list[sub] at the beginning of the subprogram or end?
3. Pass-by-Value-Result (inout Mode)
* A combination of pass-by-value and pass-by-result
* Sometimes called pass-by-copy
* Formal parameters have local storage
* Disadvantages:
- Those of pass-by-result
- Those of pass-by-value
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 (compared to pass-by-value) to formal parameters
- Potentials for unwanted side effects (collisions)
- Unwanted aliases (access broadened)
fun(total, total); fun(list[i], list[j]; fun(list[i], i);
5. Pass-by-Name (Inout Mode)
- By textual substitution
- Formals are bound to an access method at the time of the call, but actual binding to a value or address takes place at the time of a reference or assignment
- Allows flexibility in late binding
- Implementation requires that the referencing environment of the caller is passed with the parameter, so the actual parameter address can be calculated
Implementing Parameter-Passing Methods
- In most languages parameter communication takes place thru the run-time stack
- Pass-by-reference are the simplest to implement; only an address is placed in the stack
Design Considerations for Parameter Passing
* Two important considerations
- Efficiency
- One-way or two-way data transfer
* But the above considerations are in conflict
- Good programming suggest limited access to variables, which means one-way whenever possible
- But pass-by-reference is more efficient to pass structures of significant size
Overloaded Subprograms
An overloaded subprogram is a subprogram that has the same name as another subprogram in the same referencing environment. C++, Java, Ada, and C# include predefined overloaded subprograms.
a closure is a subprogram and the referencing environment where it was defined. Closures are not useful, so such languages do not support them.
A coroutine is a special kind of subprogram. Rather than the master-slave relationship between a caller and a called subprogram that exists with conventional subprograms, caller and called coroutines are more equitable. In fact, the coroutine control mechanism is often called the symmetric unit control model. Coroutines can have multiple entry points, which are controlled by the coroutines themselves. They also have the means to maintain their status between activations. This means that coroutines must be history sensitive and thus have static local variables. Secondary executions of a coroutine often begin at points other than its beginning. Because of this, the invocation of a coroutine is called a resume rather than a call.
Komentar
Posting Komentar