Chapter 6. User-Defined Functions
134
APL provides an extensive set
of
primitive functions; nevertheless,
you
may
want
a function
to
solve a special problem. APL provides a way
to
create a new func-
tion, called
function definition. During function definition,
you
use existing APL
functions
to
create new functions called user-defined functions.
Normally,
the
5100
is
in
execution mode;
that
is, after a line has been entered
and
the
EXECUTE key pressed,
the
5100
executes
that
line.
To
define a func-
tion,
the
mode must be changed
to
function definition mode; after
the
function
is
defined,
the
mode must be changed back
to
execution mode before
the
func-
tion can be executed. The mode
is
changed by entering
the
V (del) symbol. The
first V changes
the
mode
to
function definition mode;
the
second V indicates
the
end
of
function definition and changes
the
mode back
to
execution mode.
No
statement
error checking
is
performed during function definition mode.
That
is,
all
error checking
is
performed when
the
statement
is
executed.
MECHANICS
OF
FUNCTION
DEFINITION
The following steps are required
to
define a new function:
1. Enter a V followed by
the
function header (see Function Header in this
chapter). After
the
function header
is
entered, APL responds with a
[1] and waits
for
the
first
statement
of
the
function
to
be entered:
V'HDME
SeD
RE
v I B I
TO
I~
(function header)
[1]
2. Enter
the
statements
that
define
the
operations
to
be performed by the
function.
As
each line
is
entered, APL automatically responds with the
next
line number:
[:1.
:I
[2]
[:-3:1
[1.1·]
'iJ
HOME
SC()I~E
'THE
FINAL
+/HDME
'TO'
+/VISITOR
VIBIT()I~
SCDRE
IS:
Note: During function definition mode,
the
print width (see 0
PW
system vari-
able
in
Chapter 5)
is
automatically set
to
390. The
print
width returns
to
its
original value when
the
function
is
closed. This prevents problems
that
occur
when editing
statements
that
exceed
the
print
width. Editing
statements
are
discussed later
in
this chapter.
If
a user-defined function contains a
statement
that
is
greater
than
115
characters
in
length,
that
statement
cannot
be edited and
the
function
cannot
be
written
on
tape. (See OCR and OFX in Chapter 5 for
information
on
changing a user-defined function
to
a matrix.)