XLISP > XLISP 2.0  -  Contents  -  Reference  -  Previous | Next

&optional


Type:   -   keyword
Source:   -   xleval.c

Syntax

&optional [opt-arg | (opt-arg [opt-value [supplied-p]])] ...
opt-arg - optional argument
opt-value - optional argument initialization value
supplied-p - optional argument existence variable

Description

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 'supplied-p' variable, if specified, will contain  T  if the 'opt-arg' was supplied by the function call and NIL if it was not supplied by the function call. This 'supplied-p' variable 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.

Examples

(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"

See the &optional keyword in the XLISP 2.0 manual.

  Back to Top


XLISP > XLISP 2.0  -  Contents  -  Reference  -  Previous | Next