
Hey, Devs!
Do you know what an arity of a function is?
In math, the arity of a function represents the number of arguments of a function.
On programming is not different, an arity is also the representation of the number of its positional params.
When we have a function:
WithNo arguments, we say it has the arity 0 and can be called aNullary function.
With only1 argument, we say it has the arity 1 and can be called aUnary Function.
With2 arguments, we say it has the arity 2 and can be called aBinary function.
With3 arguments, we say it has the arity 3 and can be called aTernary function.
Withmore than 3 arguments, now we can be called according to the quantity of the arguments post-fixed with
ary
.
Eg.:4-ary
,5-ary
,6-ary
.
Let's code with Elixir...
defmoduleExampledodefsay(),do:"Hey I am here!"defsay(name),do:"Hey#{name}, I am here!"defsay(first_name,last_name),do:"Hey#{first_name}#{last_name}, I am here!"end
The arity can be represented like that:function/number of arity
Eg.: When we executed the functionExample.say/0
Example.say()-->"Hey I am here!"# Arity: say/0# -------------------
Eg.: When we executed the functionExample.say/1
Example.say("Diego")-->"Hey Diego, I am here!"# Arity: say/1# -------------------
Eg.: When we executed the functionExample.say/2
Example.say("Diego","Novais")-->"Hey Diego Novais, I am here!"# Arity: say/2
I hope that makes sense to you! And see you in the next content.
Contacts
Email:contato@diegonovais.com.br
Linkedin:https://www.linkedin.com/in/diegonovais/
Twitter:https://twitter.com/diegonovaistech
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse