Clojure - Variadic Functions

Variadic functions are functions that take varying number of arguments (some arguments are optional). Function can also specify the ‘&’ ampersand symbol to take in an arbitrary number of arguments.

Following example shows how this can be achieved.

(defn demo 
   [message & others]
   (str message (clojure.string/join " " others)))

The above function declaration has the ‘&’ symbol next to the argument others, which means that it can take an arbitrary number of arguments.

If you invoke the above function as


(demo "Hello" "This" "is" "the" "message")


Following will be the output.

“HelloThis is the message”

The ‘clojure.string/join’ is used to combine each individual string argument, which is passed to the function.