defun

defun (short for "define function") is a macro in the Lisp family of programming languages that defines a function in the global environment[1] that uses the form:

(defun <function-name> (<parameter1><parameter2>...<parameterN>) 
  functionbody)

Defining the function addnumbers that adds two numbers:

 ;; Define a function that adds two numbers together:
 (defun addnumbers (number1 number2)
   (+ number1 number2))
 (addnumbers 5 4)
 9

defining function square that squares a number:

 ;; Define a function that squares a number:
 (defun square (x) 
   (* x x))

and defining a function that returns the factorial of a number:

(defun factorial (number)
  (if (= number 1)
      1
    (* number (factorial (- number 1)))))
(factorial 6)
720

See also

References

  1. Macro DEFUN

External links

The Wikibook Common_Lisp has a page on the topic of: Defining functions in Common Lisp


This article is issued from Wikipedia - version of the 7/22/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.