Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Arity Of a Function
Diego Novais
Diego Novais

Posted on • Edited on

     

Arity Of a Function

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 withary.
    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
Enter fullscreen modeExit fullscreen mode

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# -------------------
Enter fullscreen modeExit fullscreen mode

Eg.: When we executed the functionExample.say/1

Example.say("Diego")-->"Hey Diego, I am here!"# Arity: say/1# -------------------
Enter fullscreen modeExit fullscreen mode

Eg.: When we executed the functionExample.say/2

Example.say("Diego","Novais")-->"Hey Diego Novais, I am here!"# Arity: say/2
Enter fullscreen modeExit fullscreen mode

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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Senior Software Engineer | Ruby | Ruby On Rails | Elixir | Phoenix | Technical Writer | LLM
  • Location
    Brazil
  • Joined

More fromDiego Novais

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp