User Tools

Site Tools


ch2_1_defun

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
ADDER                ; DEFUN returns the symbol naming the new function   

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).

Optional parameters

Since LabLISP version 1.3, it is possible for user to define function with variable number of parameters, using a symbols &OPTIONAL and &REST in the list of parameters.

(defun foo (a b &optional c d)  ...  ; arguments A, B must be given, C, D are optional 

Such definition will create function with minimum 2 and maximum 4 parameters. If the the optional parameters are not supplied, when calling FOO, they will have NIL value. It is possible to define initial value, if we need the optional parameter having some other value in such case:

>(defun adder (a b &optional (c 0))  ; arguments A, B are mandatory, C is optional
   (+ a b c))                        ; C defaults to 0, so the + will work
ADDER                

Eventually, the initializer can contain additional parameter, which will indicate, whether the user actually supplied the optional argument.

>(defun adder (a b &optional (c 0 d))  ; arguments A, B are mandatory, C is optional
   (if d (print "c supplied") (print "c not supplied")) ; D indicates if C was given
   (+ a b c))                       
ADDER                
>(adder 1 2)
"c not supplied"
3
>(adder 1 2 0)
"c supplied"
3

Functions can as well be defined with unlimited number of parameters with the &REST symbol.

>(defun adder (&rest a)      ; parameter A represent any number of arguments
   (apply #'+ a))            ; A is a list 
ADDER
>(adder 1 2 3 4 5)
15

The function will receive any number of arguments as list A.

The mandatory, optional and rest paramters can be combined. Order of the keywords &OPTIONAL and &REST must be kept. That means we can have any number of mandatory parameters, then &OPTIONAL keyword, after any number of optional parameters, with or without initializers, then &REST, which must be followed by exactly one symbol.

Lexical closure

Functions have lexical closure. In simple terms, it means that when called, the function body expressions are evaluated in the environment, where the function was defined, and not in the environment where called.

>(let ((a 1))                        ; A is local variable
   (defun add-a (b) (+ a b))         ; adds the argument B to the stored A
   (defun set-new-a (x) (setq a x))) ; changes the stored A  
SET-NEW-A                            ; LET form returns the result

The two functions share the lexical environment LET, that means they both can access the same A symbol.

>(add-a 3)             ; calling the new function
4                      ; it adds 3 to the stored A
 
>(set-new-a 5)         ; now the stored value is changed
5
 
>(add-a 3)
8                      ; new value A is used (5+3)
 
>(let ((a 100)) (add-a 3)) ; local variable A=100 is not used by the function ...
8                          ; ... we still use the 5
ch2_1_defun.txt · Last modified: 2022/03/31 04:57 by admin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki