Function Arity In Elixir

Image for post
Image for post

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:

Image for post
Image for post

In the example above, we have two functions, with arity 0 and one with arity one, .

Start IEx and run .

Image for post
Image for post

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.

Image for post
Image for post

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

Originally published at https://blog.tentamen.eu on June 5, 2020.

Written by

Founder of Tentamen, software testing agency.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store