Arity and Variadic Functions
Learn about Clojure functions that might receive different numbers of arguments but do the same thing,
We'll cover the following
The arity of a function is related to the number of arguments that the function accepts. So, for example, the function (sum-numbers a b)
has an arity equal to 2 because it receives two values, a
and b
.
Multi-arity functions
In Clojure, as in many other programming languages, we can create functions with multiple possible arities, so the same function might receive one, two, or N arguments, depending on the declaration that we do. In other programming languages, this feature is also known as function overloading.
When we have multi-arity functions, we can make one overload call another, so we can use this technique to set default argument values. And as in any other feature in Clojure, we have parentheses to isolate each new arity.
Let’s see an example:
Get hands-on with 1200+ tech skills courses.