TL;DR
Arity means how many input parameters Elixir function has. This post is part of the functional language series, and it is based on the remarkable book Elixir In Action by Sasa Juric.
In Elixir, function uniqueness is defined with:
- Module name
- Function name
- Function arity
That takes us to function name overloading. I heard the first time verb overloading in C++.
Overload [verb] load with too great a burden or cargo.
In Elixir, we can have two functions in the same module with the same name, but with different arity:
In the example above, we have two Salary.Contract.developer
functions, with arity 0 Salary.Contract.developer/0
and one with arity one, Salary.Contract.developer/1
.
Start IEx and run Salary.Contract.developer
Salary.Contract.developer 0.22
.
Remember how arity is documented with /
because you will read a lot of Elixir documentation.
We use Elixir function arity overloading in practice so we have a function with the largest arity with all the code, and functions with lower arity would call that “largest” function.
In Elixir code, you will also find notation for parameter default values.
Remember, when you use default values using \\
you will create at least two functions with the same function definition. The reason is that we get more functionality with less coding.
When you omit rate parameter in a function call, the rate gets value after \\
Remember
- what is arity
- how arity is documented
- how to use function overlading in practice
- default values