Tutorial by Examples

(defun foobar (x y) (format t "X: ~s~@ Y: ~s~%" x y)) (foobar 10 20) ; X: 10 ; Y: 20 ;=> NIL
Optional parameters can be specified after required parameters, by using &OPTIONAL keyword. There may be multiple optional parameters after it. (defun foobar (x y &optional z) (format t "X (~s) and Y (~s) are required.~@ Z (~s) is optional.~%" x y z)) ...
Global named functions are defined with DEFUN. (defun foobar () "Optional documentation string. Can contain line breaks. Must be at the beginning of the function body. Some will format the docstring so that lines are indented to match the first line, although the built-in DESCRIBE-func...
A single rest-parameter can be given with the keyword &REST after the required arguments. If such a parameter exists, the function can take a number of arguments, which will be grouped into a list in the rest-parameter. Note that the variable CALL-ARGUMENTS-LIMIT determines the maximum number of...
The &AUX keyword can be used to define local variables for the function. They are not parameters; the user cannot supply them. &AUX variables are seldomly used. You can always use LET instead, or some other way of defining local variables in the function body. &AUX variables have the a...
Functions always establish a block around the body. This block has the same name as the function name. This means you can use RETURN-FROM with this block name to return from the function and return values. You should avoid returning early whenever possible. (defun foobar (x y) (when (oddp x) ...
Keyword parameters can be defined with the &KEY keyword. They are always optional (see the Optional Parameters example for details of the definition). There may be multiple keyword parameters. (defun foobar (x y &key (z "Default" zp)) (format t "X (~s) and Y (~s) are requi...

Page 1 of 1