Calling Functions

To call a function in Clojure you use the name of the function as the first element of a list.

In this simple example, a function is defined that takes no arguments, then that function is called.

  1. (defn my-function []
  2. (str "I only return this string"))
  3. (my-function)

Functions can be defined to take arguments.

Arity

This is the term to describe the number of arguments a function takes. This can be a fixed number or variable number of arguments.

Simple polymorphism can also be used to have one function take different numbers of arguments, as with the multi-arity function in the examples below.

  1. (defn single-arity []
  2. (str "I do not take any arguments"))
  3. (defn single-arity [argument]
  4. (str "I take 1 argument only"))
  5. (defn triple-arity [argument1 argument2 argument3]
  6. (str "I take 3 arguments only"))
  7. (defn multi-arity
  8. ([argument]
  9. (str "I match 1 argument only"))
  10. ([argument1 argument2]
  11. (str "I match when 2 arguments are used")))
  12. (defn variable-arity [argument & more-arguments]
  13. (str "I assign the first argument to argument,
  14. all other arguments to more-arguments"))