Defines a named function, usually identified by a letter or digit, which may be arithmetic or written in Assembly according to companion keywords
Implemented by: dartmouth1, ansimin, ansifull, hptsb, altair8K, Applesoft, TI994, CBM, msatari, level3, colorext, spectrum, MSX, bwbasic, bw32, ABasiC, VBDOS, minibasicjs
With variations: decbasic, altair12K (see FN), tasc (does not allow redefinition of a function), trsdisk (with FN and USR), BBC (with FN or PROC), GWBASIC, QBasic (also with SEG)
Also written as:
As originally conceived, or at least as described by the 1964 manual of Dartmouth BASIC 2, DEF allowed the programmer to create a short, mathematic function whose name "must be three letters, the first two of which are FN".
20 DEF FNS(R) = SIN(R*3.1415926) 30 PRINT FNS(60)
That's why most BASIC manuals list DEF FN as a keyword, even when FN will not be recognized as a keyword by itself and the created function must be called just like the pioneers did — but some BASICs allow a space after the FN particle, as you'll see at the Variations section below.
A user-defined function may use other functions in its formula but usually it can not use itself in recursion: most BASICs will shout out a "stack overflow" error.
So far, every documentation of BASICs consulted for this page says that DEF can not be used as an interpreter command and the DEFined functions are only valid "program-wise".
While part of the original BASIC and kept in line with its purpose, practically everything in DEF has been implemented with different allowances by every BASIC on Earth.
The 8kB version of Altair BASIC extended the naming convention to "FN" followed by any valid variable name (first a letter, then a letter or digit): a function could be named like "FNJ3", thus allowing more than 26 user-defined functions. This convention was followed by Applesoft BASIC and Commodore BASIC v2.
In some BASICs, such as Commodore BASIC v2, Microsoft GW-BASIC, MSX BASIC and Sinclair Spectrum (to be confirmed), the function call allowed for a space after FN, making the particle a keyword by itself.
Some BASICs do not require the usage of the FN particle at all for the function name. This is the case of TI BASIC.
Number, type and scope of parameters in DEF-created functions vary greatly among BASICs.
Default DEF functions are single-line arithmetic expressions. But multiline DEF blocks were supported by a number of BASICs. In these, the final delimiter of DEF block is also subject to variations.
END DEF
.A type suffix after the function name is allowed in Altair Extended BASIC: "the result of the function will be forced to the function type before the value is substituted", says its manual.
Some BASICs support DEF followed by other keywords for some (very) different purposes. Such combinations are briefly described here and more detailed in specific pages:
DEF, in lower case of course, is the keyword used for functions and procedures definition in Python — possibly by influence of BASIC.