Fundamentals of Subprograms
Fundamentals of Subprograms
Subprograms
Fundamentals of Subprograms
• A subprogram is a program called by another program to perform a specific task for the program.
• Subprograms are the fundamental building blocks of programs.
• Characteristics of subprograms:
o Each subprogram has a single entry point.
o There is only one (1) subprogram in execution at any given time.
o Control always returns to the caller when the subprogram execution terminates.
• A subprogram definition is a description of the interface and the actions of the subprogram
abstraction.
• A subprogram call is the explicit request that a specific subprogram be executed.
• A subprogram header, which is the first part of the definition, includes the name, the kind of
subprogram, and the formal parameters.
• Examples of headers:
Fortran: subroutine avg (a, b, c)
Ada: procedure Avg (A, B: in Integer; C: out Integer)
C: void avg (float a, float b, float c)
Python: def avg (a, b, c)
• The subprogram’s parameter profile, which is the first part of the definition, includes the name, the
kind of subprogram, and the formal parameters.
• Actual parameters represent values or addresses used in the subprogram call statement.
• A parameter is said to be a:
o Positional parameter: The first actual parameter is bound to the first formal parameter and
so forth.
o Keyword parameter: The name of the formal parameter to which an actual parameter is to
be bound is specified with the actual parameter in a call.
• A default value is used if no actual parameter is passed to the formal parameter in the subprogram
header.
• Two (2) categories of subprograms:
o Procedures provide user-defined parameterized computation statements.
o Functions are semantically modeled on mathematical functions.
Parameter-Passing Methods
• A parameter-passing method is a mechanism in which a parameter is transmitted to and/or from a
called subprogram.
• Three (3) semantic models of formal parameters:
o In mode: They can receive data from the corresponding actual parameter.
o Out mode: They can transmit data to the actual parameter.
o Inout mode: They can do both.
Other Concepts
• An overloaded subprogram is a subprogram that has the same name as another subprogram in the
same referencing environment.
• An overloaded subprogram must be different from the others in terms of number, order, types of its
parameters, and return type (for functions).
• A polymorphic subprogram takes parameters of different types on different activations.
• Ad hoc polymorphism is a kind of polymorphism provided by overloaded subprograms.
• Subtype polymorphism occurs when a variable of a type can access any object of that type or any
type derived from it.
• Parametric polymorphism is provided by a subprogram that takes generic parameters that are used
in type expressions that describe the types of the parameters of the subprogram.
• Parametrically polymorphic subprograms are called generic subprograms.
• A closure is a subprogram and the referencing environment where it was defined.
• Example of a closure in JavaScript:
function adder(x) {
return function(y) {return x + y;}
}
...
var add10 = adder(10);
var add5 = adder(5);
document.write(“Add 10 to 20: ” + add10(20) + “<br />”);
document.write(“Add 5 to 20: ” + add5(20) + “<br />”);
• A coroutine is a special program that has multiple entries. It can be used to provide interleaved
execution of subprograms.
• A skeletal coroutine:
sub co1(){
...
resume co2();
...
resume co3();
...
}
References:
Ben-Ari, Mordechai (2006). Understanding programming languages. Chichester: John Wiley & Sons, Inc.
Sebesta, Robert W. (2015). Concepts of programming languages. (11th ed.). USA: Pearson Education, Inc.
Tucker, Allan B. and Noonan, Robert E. (2002). Programming languages: Principles and paradigms. (2nd ed.).
New York: Mc-Graw Hill