Subprogram
Subprograms
are process abstraction. Each subprogram
has an entry point. The calling program
unit is suspended during the execution of the called subprogram, which implies
that there is only one subprogram in execution at any given time, and ontrol always returns to the caller when the subprogram
execution terminates.
There are
many forms of subprograms: procedure or subroutine, function, module, etc. The following is an example of a procedure
subprogram in a program in PL/0.
var
x; (* declaration *)
var
y;
procedure myproc; (*
subprogram *)
var
temp;
begin
temp := 100
end;
begin (* main
program statements*)
x := 10;
call myproc;
y := x
end.
The program begins
its execution with the declaration statements (var x;
var y;). Control of the
program is then transferred to the main program statements. When the procedure call statement (call myproc;)
is executed, the main program is suspended and control passes to the
procedure. The main program saves its
execution status on the run time stack and transfers control to the procedure (myproc) code. An
activation record is generated that contains the local variables, parameters,
and information to transfer control back to the calling program (main) at the
completion of the procedure. When the execution of the procedure terminates,
the main program restores its execution status and continue execution at the
statement immediately after the procedure call.
Parameter
Passing
There are
three modes of formal parameter semantics:
·
In-mode:
formal parameters receive data from corresponding actual parameters
·
Out-mode: formal parameters transmit data to actual
parameters
·
Inout-mode: in both in mode and out mode
Pass-By-Value
- the value of actual parameter is copied to the corresponding
formal parameters
- parameters act as local variables
- in-mode semantics
Pass-By-Result
- no value is transmitted to the subprogram
- value of formal parameter is copied back to the corresponding
actual parameter on subprogram returns
- out-mode semantics
Pass-By-Value-Result
- combination of pass-by-value and pass-by-result
- inout-mode
semantics
Example
int i =
3; /*i is
global */
void
fun(int a, int b) { i = b; }
void
main()
{
int
list[10];
list[i] = 5;
fun(i, list[i]);
}
Action of fun
addr_i
= &i
addr_listi
= &list[i]
a = *addr_i
b = *addr_listi
i
= b
*addr_i = a
*addr_listi = b
Pass-By-Reference
- transmits
an access path, usually just an address, to the subprogram
- inout mode semantics
- efficient in time and space.
Duplicate space is not required, nor is any copying.
- slower access to formal parameters because of indirect
addressing
- erroneous changes may be made to the actual parameters
Pass-By-Name
- actual parameter is textually substituted for the
corresponding formal parameter in all its occurrences in the subprogram
- inout-mode
semantics
Example
procedure
BIGSUB;
integer GLOBAL;
integer array
LIST[1:2];
procedure
SUB(PARAM);
integer PARAM;
begin
PARAM := 3;
GLOBAL := GLOBAL
+ 1;
PARAM := 5;
end;
begin
LIST[1] := 2;
LIST[2] := 2;
GLOBAL := 1;
SUB(LIST[GLOBAL])
end;
When the
procedure call (SUB(LIST[GLOBAL])) is made, PARAM is bound to
LIST[1] because GLOBAL is
1. The statement PARAM := 3; is changed to LIST[1]
:= 3; which assign the value
3 to LIST[1].
The statement GLOBAL :=
GLOBAL + 1; changes the
value of GLOBAL to 2.
The statement PARAM :=
5; is changed to LIST[2] := 5; which assign the value 5 to LIST[2]. The result is that both LIST[1] and LIST[2] are changed.
Parameters that are Subprogram Names
Example – JavaScript
<html>
<head>
<title>Parameters That are Subprogram Names</title>
<script
language="JavaScript">
<!--
function
sub1() {
var
x;
function sub2() {
alert(x);
};
function sub3() {
var
x;
x = 3;
sub4(sub2);
};
function sub4(subx) {
var
x;
x = 4;
subx();
};
x = 1;
sub3();
};
//-->
</script>
</head>
<body>
<script
language="javaScript">
<!--
sub1();
//-->
</script>
</body>
</html>
Generic Subprograms
A generic subprogram takes parameters of different
types on different activations.
Example – Generic function in C++
Template
<class Type>
Type max(Type first, Type second)
{
return first >
second ? first : second;
}
Coroutine
Coroutines invoke each other in programming execution. They often partially execute and then
transfer control to some other coroutine. When restarted, a coroutine
resumes execution just after the statement it used to transfer control
elsewhere.
Practice
1. Search the Internet to find an example program that uses coroutines.
Reference
1. Concepts of Programming Languages, Chapter 9.