Chapter 6. User-Defined Functions
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 (dell 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.
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
[I] and waits for the first statement of the function to be entered:
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:
Mote: 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 11 5 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.)
I
1 34
3.
Enter another V when the function definition is complete. The closing V
may be entered alone or at the end of a statement. For example:
Note: If the closing V is entered at the end of a comment statement, which
begins with a R symbol, the V will be treated as part of the comment and
the function will not be closed.
Function Header
The function header names the function and specifies whether a function has no
arguments (niladic), one argument (monadic), or two arguments (dyadic).
Note: Function names should not begin with SA or TA, because SA and TA are
used for stop and trace control (Stop Control and Trace Control are discussed
later in this chapter).
The function header also determines whether or not a function has an explicit
result. If a function has an explicit result, the result of the function is tempor-
arily stored in a result variable (names in the function header) for use in calcula-
tions outside the function. The result variable must be included in the result
statement (the statement that determines the final result of the function) as well
as the function header. For example:
Result Variable
-
P I? I::: ::i i.1 I.., 'r 4.. x 13 I., 11 ::i Y
'7
J '7
The result of the function is
1, il+3 l:)l..,I..l$i I././ temporarily stored in the re-
sult variable so that it can
be used by another function.
User-defined functions that do not have an explicit result cannot be used as part
of another expression. For example:
Previous Page Next Page