User Tools

A PCRE internal error occured. This might be caused by a faulty plugin

====== Macro DEFUN ====== ====Syntax==== * **defun** //function-name lambda-list <nowiki>[[</nowiki>declaration''*'' | documentation<nowiki>]]</nowiki> form''*''// → //function-name// ====Arguments and Values==== * //function-name// - a //[[CL:Glossary:function name]]//. * //lambda-list// - an //[[CL:Glossary:ordinary lambda list]]//. * //declaration// - a **[[CL:Special Operators:declare]]** //[[CL:Glossary:expression]]//; not evaluated. * //documentation// - a //[[CL:Glossary:string]]//; not evaluated. * //forms// - an //[[CL:Glossary:implicit progn]]//. * //block-name// - the //[[CL:Glossary:function block name]]// of the //function-name//. ====Description==== Defines a new //[[CL:Glossary:function]]// named //function-name// in the //[[CL:Glossary:global environment]]//. The body of the //[[CL:Glossary:function]]// defined by **defun** consists of //forms//; they are executed as an //[[CL:Glossary:implicit progn]]// when the //[[CL:Glossary:function]]// is called. **defun** can be used to define a new //[[CL:Glossary:function]]//, to install a corrected version of an incorrect definition, to redefine an already-defined //[[CL:Glossary:function]]//, or to redefine a //[[CL:Glossary:macro]]// as a //[[CL:Glossary:function]]//. **defun** implicitly puts a **[[CL:Special Operators:block]]** named //block-name// around the body //forms// (but not the //[[CL:Glossary:forms]]// in the //lambda-list//) of the //[[CL:Glossary:function]]// defined. //documentation// is attached as a //[[CL:Glossary:documentation string]]// to //name// (as kind **[[CL:Types:function]]**) and to the //[[CL:Glossary:function]]// //[[CL:Glossary:object]]//. Evaluating **defun** causes //function-name// to be a global name for the //[[CL:Glossary:function]]// specified by the //[[CL:Glossary:lambda expression]]// <blockquote> ([[CL:Macros:lambda]] //lambda-list <nowiki>[[</nowiki>declaration''*'' | documentation<nowiki>]]</nowiki>// ([[CL:Special Operators:block]] //block-name// form''*'')) </blockquote> processed in the //[[CL:Glossary:lexical environment]]// in which **defun** was executed. (None of the arguments are evaluated at macro expansion time.) **defun** is not required to perform any compile-time side effects. In particular, **defun** does not make the //[[CL:Glossary:function]]// definition available at compile time. An //[[CL:Glossary:implementation]]// may choose to store information about the //[[CL:Glossary:function]]// for the purposes of compile-time error-checking (such as checking the number of arguments on calls), or to enable the //[[CL:Glossary:function]]// to be expanded inline. ====Examples==== <blockquote> ([[CL:Macros:defun]] recur (x) ([[CL:Macros:when]] ([[CL:Functions:math-greater|>]] x 0) (recur ([[CL:Functions:one-minus|1-]] x)))) <r>RECUR</r> ([[CL:Macros:defun]] ex (a b &optional c (d 66) &rest keys &key test (start 0)) ([[CL:Functions:list]] a b c d keys test start)) <r>EX</r> (ex 1 2) <r>(1 2 NIL 66 NIL NIL 0)</r> (ex 1 2 3 4 :test '[[CL:Functions:equal]] :start 50) <r>(1 2 3 4 (:TEST [[CL:Functions:EQUAL]] :START 50) [[CL:Functions:EQUAL]] 50)</r> (ex :test 1 :start 2) <r>(:TEST 1 :START 2 NIL NIL 0)</r> </blockquote> The below function assumes its callers have checked the types of the arguments, and authorizes the compiler to build in that assumption. <blockquote> ([[CL:Macros:defun]] discriminant (a b c) ([[CL:Special Operators:declare]] ([[CL:Types:number]] a b c)) "Compute the discriminant for a quadratic equation." ([[CL:Functions:math-subtract|-]] ([[CL:Functions:math-multiply|*]] b b) ([[CL:Functions:math-multiply|*]] 4 a c))) <r>DISCRIMINANT</r> (discriminant 1 2/3 -2) <r>76/9</r> </blockquote> The below function assumes its callers have not checked the types of the arguments, and performs explicit type checks before making any assumptions. <blockquote> ([[CL:Macros:defun]] careful-discriminant (a b c) "Compute the discriminant for a quadratic equation." ([[CL:Macros:check-type]] a [[CL:Types:number]]) ([[CL:Macros:check-type]] b [[CL:Types:number]]) ([[CL:Macros:check-type]] c [[CL:Types:number]]) ([[CL:Special Operators:locally] ([[CL:Special Operators:declare]] ([[CL:Types:number]] a b c)) ([[CL:Functions:math-subtract|-]] ([[CL:Functions:math-multiply|*]] b b) ([[CL:Functions:math-multiply|*]] 4 a c)))) <r>CAREFUL-DISCRIMINANT</r> (careful-discriminant 1 2/3 -2) <r>76/9</r> </blockquote> ====Affected By==== None. ====Exceptional Situations==== None. ====See Also==== * **[[CL:Special Operators:flet|Special Operator FLET]]** * **[[CL:Special Operators:labels|Special Operator LABELS]]** * **[[CL:Special Operators:block|Special Operator BLOCK]]** * **[[CL:Special Operators:return-from|Special Operator RETURN-FROM]]** * **[[CL:Special Operators:declare|Special Operator DECLARE]]** * **[[CL:Functions:documentation|Generic Function DOCUMENTATION]]** * {\secref\Evaluation} * {\secref\OrdinaryLambdaLists} * {\secref\DocVsDecls} ====Notes==== **[[CL:Special Operators:return-from]]** can be used to return prematurely from a //[[CL:Glossary:function]]// defined by **defun**. Additional side effects might take place when additional information (typically debugging information) about the function definition is recorded. \issue{COMPILE-FILE-HANDLING-OF-TOP-LEVEL-FORMS:CLARIFY} \issue{DECLS-AND-DOC} \issue{FUNCTION-NAME:LARGE} \issue{DEFMACRO-BLOCK-SCOPE:EXCLUDES-BINDINGS} \issue{DOCUMENTATION-FUNCTION-BUGS:FIX}