This is an old revision of the document!
DEFUN
DEFUN
form defines a named ordinary function object in global scope. It needs at least one symbol argument, which is not evaluated and will be the name of the function. Defun with no other arguments creates a function that evaluates to nil. For any practical use, we need to supply more arguments. Second argument must then be a list of parameters (symbols) of the function, the symbols are not evaluated and will be used for local binding of parameters once the defined function is called. In fact, no parts of the DEFUN
expression are evaluated during the definition. Third argument is optional description string. If the third argument is not string, it is considered first form of the function body. Any other parameters are to DEFUN
are stored as the function body.
(defun adder (a b) ; function named ADDER, arguments A, B “replaces plus” ; optional description string (+ a b)) ; body expression
In this example we have created function adder. Its body contains single arithmetic function. The newly defined function can be called as any other function. Result of the function adder will be result of the +
function.
>(adder 2 4) 6
If function body contains more expressions, all are evaluated sequentially (once the function is called), but only the output of the last one is returned from the function (this behavior is due to implicit PROGN
).
In LabLisp it is not (yet) possible for user to define function with variable number of parameters. But some of the built-in functions support that (e.g. +
or LIST
), and it is planned feature for user defined functions too.