Defun
Encyclopedia
defun is a macro in the Lisp family of programming languages that defines a function
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....

 in the global environment
Global variable
In computer programming, a global variable is a variable that is accessible in every scope . Interaction mechanisms with global variables are called global environment mechanisms...

 that uses the form:

(defun (...)
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
Factorial
In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n...

of a number:

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

See also

  • define
  • defmacro
  • lambda in Python
    Python (programming language)
    Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

     and Lisp

External links

  • defun in Emacs Lisp
    Emacs Lisp
    Emacs Lisp is a dialect of the Lisp programming language used by the GNU Emacs and XEmacs text editors . It is used for implementing most of the editing functionality built into Emacs, the remainder being written in C...

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK