next up previous
Contents Next: do (SPECIAL FORM) Up: Appendix: Selected Lisp Previous: cons (FUNCTION)

defun (MACRO)

Format: (defun <name> (<par1> <par2> ... <parN> ) <body> )

Required arguments: 2

<name> : a symbol which is appropriate as the name of function; (<par1> <par2> ...): a list of zero or more symbols which are appropriate as parameter names

Optional arguments: 1

<body> : a sequence of zero or more Lisp expressions

The arguments to defun are not evaluated---they are used to establish a procedure definition. The first argument is a symbol which specifies the name of the function. This name can later be used to execute the <body> of the function. The parameter-list follows the name. This list specifies the number and order of arguments in a function call. Each <par> is symbol which may appear in the <body> . The value of each parameter, <par> , is determined by the value of corresponding argument in the function call. defun returns the name of the function.


>  (defun square (x)
     (* x x))

>  (square 4)

>  (square (+ 7 -2))

>  (defun equal-length (lst1 lst2)
     (cond ((= (length lst1) (length lst2)) t)
           (t nil)))

>  (equal-length '(a b c) '((d e) f g))

>  (equal-length '() (rest '(a b)))

>  (defun side-effect (x y)
     (setq x (* x x x))
     (+ x y))

>  (side-effect 2 3)

>  (side-effect -4 14)

© Colin Allen & Maneesh Dhagat
March 2007