Defined function (traditional)


 * This page is about the function form typically written with the Del character. See Function styles for an overview of all forms; in particular dfns are considered a type of defined function in Dyalog APL and GNU APL.

A user-defined function (or tradfn, pronounced "trad fun", for "traditional function", in Dyalog APL) is a function defined using a header that includes the function's name. Introduced in APL\360, function definition was universally supported by APL dialects for much of the language's history, and is still commonly used in mainstream APLs. Since the 1990s other ways to describe functions have appeared, with J and K rejecting function definition in favor of anonymous function description.

The canonical representation form is equivalent to what the user would type into the line editor to define the function. An alternative representation consists of the entire session log transcript, including Dels and line numbers, after having such a definition has been made.

Beginning in the 2010s Dyalog-based APL dialects including ngn/apl, dzaima/APL, and APL\iv have removed function definition in favor of dfns. Wikipedia has a comparison of dfns versus tradfns.

In many dialects the function header syntax of defined functions is adapted to allow user-defined operators as well. A tradfn operator can also be called a tradop (pronounced "trad op"), parallel to how a "dfn" operator can be called a dop.

Basics
A simple dyadic function:

Braces
An ambivalent function with an optional left argument, a conditional control structure, one local variable, and a shy result:

Brackets
GNU APL allows functions and operators to accept an axis argument:

Operators
A monadic operator and a dyadic operator, both deriving monadic functions:

A monadic operator and a dyadic operator, both deriving dyadic functions:

Properties
Tradfns allow both functional and procedural programming, and are essential for object-oriented programming. They support a full set of keywords for flow control and object declarations.

One of the most noticeable differences from dfns is that tradfns must declare their locals, because assignments are global by default. The declaration is done in a header line which also determines the function's name and calling syntax. Dyalog APL adds a handful of extensions this (see below).

Tradfns cannot be nested but can contain dfns. A tradfn can dynamically create another tradfn by "fixing" its source, and if the function thus created has a name which has been localised, the inner function will only exist the scope of the outer function. Nested functions are less necessary due to tradfns using dynamic scoping as opposed to the lexical scoping of dfns. In other words, a tradfn can "see" locals of its caller.

A tradfn can be niladic which causes it to behave syntactically like an array. However, every time its name is referenced, it will run to create a result (if any). Such methods are often used to return a cache or as an entry point for the user.

Dyalog APL extensions
Dyalog APL's tradfns are enhanced in various ways compared to most other dialects.

Dynamic localisation
Names can be localised dynamically, while a tradfn is running, using the Shadow Name system function.

Locals lines
Additional local names can also be declared on separate lines following the header line:

Explicit ambivalence
The the header syntax can explicitly specify that a function is ambivalent by enclosing the left argument name in curly braces (for example ). Others dialects treat all functions that declare a left argument name as ambivalent and leave it up to the programmer to check for the presence of a value.

Function results
A tradfn can return a function value as result. The returned function will replace the function and its arguments (if any) in the calling expression:

Shyness
A tradfn can be declared as shy, that is, it exhibits the same behaviour as an assignment, in that by default, no result is printed when the function terminates, but attempting to use the result still succeeds. This declaration is done by putting curly braces around the result name (for example ).

Namelists
The right argument and the result can be a name list instead of single name. The interpreter will unpack the right argument when the function is called, and collect the result when the function returns.

A+ differences
A+ uses a reworked style of function and operator definition than maintains the principle of a header that matches the way the function will be used, but differs in many details:
 * The result name is not included in the header; instead, the result of the last executed statement is returned (and so functions that do not return a result cannot be defined).
 * The header is separated from the body with a colon, and the body of a multi-line function is enclosed in curly braces.
 * Functions have lexical scope. Variables assigned are local by default, and can be made global by enclosing their names in parentheses when assigning.

Tutorials

 * APL Cultivation: tradfns
 * Bernard Legrand. Mastering Dyalog APL (page 176). Dyalog Ltd. November 2009.

Documentation

 * Dyalog: Functions, Operators
 * APLX: User-defined Functions, User-defined Operators