Arity
The arity of a function is the number of arguments that the function has. In Erlang, two functions with the same name and different arity in the same module represent entirely different functions. They have nothing to do with each other apart from a coincidental use of the same name.
By convention Erlang programmers often use functions with the same name and different arities as auxiliary functions. Here’s an example:
| lib_misc.erl | |
| | sum(L) -> sum(L, 0). |
| | |
| | sum([], N) -> N; |
| | sum([H|T], N) -> sum(T, H+N). |
What you see here are two different functions, one with arity 1 and the second with arity 2.
The function sum(L) sums the elements of a list
L. It makes use of an auxiliary routine called
sum/2, but this could have been called anything. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access