name | symbol |
args | arguments of function |
forms | sequentially executed forms |
DEFUN form creates named function. The function is associated with definition environment. Named functions can be called simply by specifying their name in function position in parenthesis, or they can be acquired by FUNCTION special form, or SYMBOL-FUNCTION function. Arguments of function can be regular (matched by position), optional (with default values), keyword (matched by keyword symbol) and rest (taking rest of arguments into a list). Result of function application is value of the last form unless return function or nonlocal exit is executed. Functions can be redefined. See also LAMBDA, FUNCALL, APPLY.
(defun myname (x) (+ x 3)) => MYNAME
(defun myname (x y) (* x y) (+ x y)) (myname 2 3) => 5
(defun myname (&optional (one 1) (two 2)) (list one two)) (myname) => (1 2) (defun myname (&optional (one 1) (two 2)) (list one two)) (myname 10) => (10 2) (defun myname (&optional (one 1) (two 2)) (list one two)) (myname 10 20) => (10 20)
(defun myname (&rest myargs) (length myargs)) (myname) => 0 (defun myname (&rest myargs) (length myargs)) (myname 4 5 6) => 3 (defun myname (&rest myargs) (length myargs)) (myname '(4 5 6)) => 1
(defun myname (&key one two) (list one two)) (myname) => (NIL NIL) (defun myname (&key one two) (list one two)) (myname :two 7) => (NIL 7) (defun myname (&key one two) (list one two)) (myname :two 7 :one 4) => (4 7)