User-defined functions are the equivalent of subroutines or functions in other programming languages. They associate a series of lines of APL code with a name chosen by the programmer.
When a function is evaluated, it performs some action on data known as an 'argument'. Functions may have no arguments, one argument, or two arguments. These three types of functions are often referred to as follows:
0 arguments Niladic 1 argument Monadic Argument on right 2 arguments Dyadic Arguments on left and right
If you defined a function called. say,
X ← SD 23 89 56 12 99 2 16 92
A function may or may not return a result.
You specify the number of arguments the function is to have, and the name of the result field (if there is one) when you define the function header of the function you are about to write.
Header line for user-defined functions
In addition to the names used for the left and right arguments and result (if applicable) which will all be 'local', the header line may also be used to localize other variables (and system variables), as well as function names. Whilst the function or operator is running, these local variables 'shadow' global variables of the same name, that is they will exclude from use a global object of the same name. System commands continue to reference the global objects. Local variables (and functions) are however themselves global to functions called within their function or operator.
The general format for a function header is:
R← A FUNCTION B;VAR1;VAR2 or A FUNCTION B;VAR1;VAR2
depending on whether or not a result is returned. R, the result, A, the left argument, B, the right argument are all optional. Local names, if any, are listed after the function name and arguments, separated from them and each other by semi-colons (;), VAR1 and VAR2 above. Comments may also appear at the end of the header line, following a
In most versions of APLX, there are two ways to create or edit a function.
The most commonly used way is to use an on-screen editor, which allows you to edit the function text very easily in an editor window. The editor is either invoked through the application's Edit menu, or with the
For backward compatibility with old APL systems, APLX also supports a primitive line-at-a-time editor called the Del (or Line) Editor. To enter definition mode and create a new function you type
For clarity, we will list functions here as though they were entered using the Del editor, where a
The function header
The first line of a function is called the function header. This example is the header for a function called
If you want the function you are defining to have arguments you must put them in the header by typing a suitable function header:
The above header specifies that
∇SD X  SUM ← +/X  AV ← SUM÷⍴X  DIFF ← AV-X  SQDIFF ← DIFF*2  SQAV ← (+/SQDIFF)÷⍴SQDIFF  RESULT ← SQAV*0.5 ∇
It's quite unimportant what the statements in the function are doing. The point to notice is that they use the variable
SD 12 45 20 68 92 108
those numbers are put in
The function header for a dyadic (two-argument) function would be defined on the same lines:
∇X CALC Y
When you subsequently use
1 4 7 CALC 0 92 3
If you want the result of a function to be put into a specified variable, you can arrange that in the function header too:
∇Z ← X CALC Y
In practice, most APL functions return a result, which can then be used in expressions for further calculations, or stored in variables.
Local and global variables
Variable names quoted in the header of a function are local. They exist only while the function is running and it doesn't matter if they duplicate the names of other variables in the workspace.
The other variables - those used in the body of a function but not quoted in the header, or those created in calculator mode - are called global variables.
It is obviously convenient to use local variables in a function. It means that if you decide to make use of a function written some time before, you do not have to worry about the variable names it uses duplicating names already in the workspace.
But to go back to the
You can 'localize' any variable used in a function by putting a semicolon at the end of the function header and typing the variable name after it:
You may wonder what happens if functions that call each other use duplicate local variable names. You can think of the functions as forming a stack with the one currently running at the top, the one that called it next down, and so on. A reference to a local variable name applies to the variable used by the function currently at the top of the stack.
Comments in functions
If you want to include comments in a function, simply enter them, preceded by a comment
∇R ← AV X  ⍝ This function finds the average of some numbers  R ← (+/X)÷⍴X ⍝ The numbers are in X ∇
There are two comments in the example above. Note that the one on line 2 doesn't start at the beginning of a line.
It is possible to lock a function. A locked function can only be run. You can not edit it or list the statements it consists of. To lock a function, edit it in the Del editor but type a
A locked function cannot be unlocked.
Local functions cannot be edited by the standard
All dyadic functions may be used monadically. If used monadically, the left argument is undefined (i.e. has a Name Classification,
∇R←A NOMADIC B  :If 0=⎕NC 'A' ⍝ DOES A EXIST?  A←5 ⍝ NO, SO WE HAVE BEEN USED MONADICALLY  :EndIf etc.
Copyright © 1996-2010 MicroAPL Ltd