type: keyword location: built-in source file: xleval.c Common LISP compatible: similar supported on: all machines
&optional [ <opt-arg> | (
[ [ ] ] ) ] ... <opt-arg> - optional argument <opt-value> - optional argument initialization <exist-symbol> - optional argument existence symbol
In XLISP, there are several times that you define a formal argument list for a body of code (like DEFUN, DEFMACRO, :ANSWER and LAMBDA). All of the formal arguments that are defined are required to appear in the invocation of the defined function or operation. If there are any &OPTIONAL arguments defined, they will be filled in order. If there are insufficient parameters for the &OPTIONAL arguments, they will contain NIL, unless the arguments have an <opt-value> initial value specified. The <exist-symbol>, if it is specified, will contain a T if the <opt-arg> was supplied by the function call and a NIL if it was not supplied by the function call. This <exist-symbol> allows the programmer to test for an arguments existence. At the end of the function or operation execution, these local symbols and their values are are removed.
(defun foo ; define function FOO (a &optional b (c 1) ) ; with some optional args (print a) (print b) (print c)) ; (foo) ; error: too few arguments (foo 1) ; prints 1 NIL 1 (foo 1 2) ; prints 1 2 1 (foo 1 2 3) ; prints 1 2 3 ; (defun fee ; define function FEE (a &optional (b 9 b-passed) ) ; with some optional args (print a) (print b) ; (if b-passed (print "b was passed") ; (print "b not passed"))) ; (fee 1) ; prints 1 9 "b not passed" (fee 1 2) ; prints 1 2 "b was passed"