CmSc 315 Programming Languages
Chapter 6: Encapsulation - Subprograms and Type Definitions
Subprograms can be viewed as abstract operations
on a predefined data set.
A subprogram represents a mathematical function that maps each
particular set of arguments
into a particular set of results.
Specification of a subprogram (same as that for a primitive operation):
Some problems in attempting to describe precisely the function computed by a subprogram:
Implementation of a subprogram:
The body is encapsulated, its components cannot be accessed separately by
the user of the subprogram. The interface with the user (the calling program)
is accomplished by means of
arguments and returned results.
Type checking: similar to type checking for primitive
operations.
Difference: types of operands and results are explicitly stated in the program
2. 1. Subprogram definitions and subprogram activations
Subprogram definition: the set of statements constituting the body of
the subprogram.
It is a static property of the program, and it is the only
information available during translation.
A simple (but not efficient) approach:
Each time the subprogram is invoked, a copy of its executable
statements,
constants and local variables is created.
A better approach:
The executable statements and constants are invariant
part of the subprogram -
they do not need to be copied for each execution of
the subprogram.
A single copy is used for all activations of the subprogram.
This copy is
called code segment. This is the static part.
The activation record contains only the parameters, results and local
data.
This is the dynamic part. It has same structure, but different values for
the variables.
Below is Figure 6.3 from the textbook.
On the left is the subprogram definition. On the right is the
activation record created during
execution. It contains the types and number of
variables used by the subprogram,
and the assigned memory locations at each
execution of the subprogram.
The definition serves as a template to create the activation record (the use
of the word template
is different from the keyword template in class definitions
in C++, though its generic meaning
is the same - a pattern, a frame to be filled
in with particular values.
In class definitions the binding refers to the data
types and it is performed at compilation time,
while here the binding refers to
memory locations and data values,
and it is performed at execution time.)
Generic subprograms: have a single name but several different definitions – overloaded.
Subprogram definitions as data objects
In compiled languages subprogram definition is separate from
subprogram execution.
– C, C++, Java
In interpreted languages there is no difference - definitions are
treated as run-time
data objects – Prolog, LISP, Perl. Interpreted
languages use an operation to invoke
translation at
run-time – consult in
Prolog, define in LISP.
Type definitions are used to define new data types.
Note,
that they do not define a complete abstract data type,
because the definitions
of the operations are not included.
Format: typedef definiion name
Actually we have a substitution of name for the definition.
Examples:
typedef int key_type; key_type key1, key2;
These statements will be processed at translation time and the type of
key1 and
key2
will be set to integer.
struct rational_number
{int numerator, denominator;};
typedef rational_number rational;
rational r1, r2;
Here r1 and r2 will be of type rational_number
Two questions to be answered:
Name equivalence
Issues
Every object must have an assigned type, there can be no anonymous types.
A singe type definition must serve all or large parts of a program.
Structural equivalence:
Issues
Do components need to be exact duplicates? Can field order be different
in records?
Can field sizes vary?
Data object equality
We can consider two objects to be equal if each member in one object is
identical
to the corresponding member of the other object. However there
still may be a problem.
Consider for example the rational numbers 1/2 and
3/6.
Are they equal according to the above definition?
In general, the compiler has no way to know how to compare data values of
user-defined type.
It is the task of the programmer that has defined that
particular data type to define also
the operations with the objects of that
type.
Parameters allow the user to prescribe the size of data types needed – array sizes.
Implementation: The type definition with parameters
is used as a template
as any other
type definition during compilation.
Exam-like questions