clojure Functions Parameters and Arity


Example

Clojure functions can be defined with zero or more parameters.

(defn welcome
    "Without parameters"
    []
    "Hello!")

(defn square
    "Take one parameter"
    [x]
    (* x x))

(defn multiplier
    "Two parameters"
    [x y]
    (* x y))

Arity

The number of arguments a function takes. Functions support arity overloading, which means that functions in Clojure allow for more than one "set" of arguments.

(defn sum-args
  ;; 3 arguments
  ([x y z]
     (+ x y z))
  ;; 2 arguments
  ([x y]
     (+ x y))
  ;; 1 argument 
  ([x]
     (+ x 1)))

The arities don't have to do the same job, each arity can do something unrelated:

(defn do-something
  ;; 2 arguments
  ([first second]
     (str first " " second))
  ;; 1 argument 
  ([x]
     (* x x x)))