Clojure defn

Clojure defn

In Clojure, the defn macro is used to define a new function. It is used to create a named function with a given name, arguments, and body. Here is the syntax for using defn:

(defn function-name
  "docstring"
  [arg1 arg2 ...]
  (function-body))
  • function-name: The name of the function.
  • "docstring": An optional string that describes the function.
  • [arg1 arg2 ...]: The arguments that the function takes.
  • (function-body): The body of the function, which contains the code that will be executed when the function is called.

Here's an example of using defn to define a simple function that adds two numbers:

(defn add
  "Adds two numbers"
  [a b]
  (+ a b))

This defines a function named add that takes two arguments a and b and returns their sum.

Please note that the defn macro is used to define functions in Clojure, and it is not a built-in function itself.