# Function arity

This post is part of Code building blocks series that focus on various elements and concepts of programming languages.

Before we dive into arity, we need to know the difference between a parameter and argument.

### Arity types

Arity of a function (or operation) describes the number of arguments that the function (or operation) takes.

We have few distinction sof arity:

• minimum arity and maximum arity.
• fixed arity and variable arity.
• definitive and indefinite arity

Let’s discuss those distinctions in more details and see how they are related to each orher.

### Minimum & maximum arity

We can define the minimum arity as the smallest number of arguments function expects and the maximum arity as the largest number of arguments that function takes.

### Definite & Fixed arity

Fixed arity function is the most popular kind present in almos tall programming languages.

Fixed arity function must be called with the same number of arguments as the number of parameters specified in its declaration.
Definite arity function must be called with a finite number of arguments.
`fun sum(a:Int, b:Int) {    //...}`
`sum(1 ,4) // arity 2sum(6 ,7) // arity 2`

sum function takes two arguments (1, 4). Minimum and maximum arity are equal to 2. We say that:

• arity of the function is 2
• sum is an function of arity 2
• sum function has binary arity

. It is called fixed arity, because sum function is always called with fixed number of arguments. We always need to supply two arguments. This function arity is also definite because its takes certain finite, known number of arguments.

### Variable arity

Variable arity is the contrast to fixed arity.

Variable arity function is a function where the number of arguments may vary in each cal.

We have two kinds of variable arity indefinite and definite.

#### Indefinite variable arity

Many modern languages allow defining a special vararg parameter (variable arity parameter). We can pass an arbitrary set of arguments without the need to use any kind of collection (array/list, etc.) as parameter type.

`//Kotlinfun sum(vararg a:Int) {    //...}`
`//Javapublic void sum(Integer... arguments){    //...     }`

varargs functions are formally known as variable arity functions. vararg parameter allows calling the function with variable, potentially unlimited number of arguments so they allow defining function of indefinite arity.

Indefinite arity function can be called with indefinite number of arguments (we can’t list all of the function arities)

Our sum function can be called with any number of arguments.

`sum() // arity 0sum(1) // arity 1sum(1, 4) // arity 2sum(1, 4, 7) // arity 3...`

The minimum arity of the sum function is defined by a range starting from 0 (minimum arity) to potentially an indefinite number of arguments (maximum arity). This why we also call it indefinite arity function.

Minumum arity does not have to be 0. While vararg parameter is usually the last parameter defined in the function signature, but we can define parameters before it.

`fun sum(a:Int, b:Int = 7, vararg arguments:Int) {    // code}`

Now Minumim arity of updated sum function equals to the number of parameters without vararg modifier (2). Maximum arity equals to the potentially indefinite number of arguments.

`sum(1, 4) // arity 2sum(1, 4, 7) // arity 3sum(1, 4, 7, 6) // arity 4...`

#### Definite variable arity

Definite variable arity is the contrast to indefinite variable arity.

Definite arity function is called with the definite number of arguments (we can list all of the function arities).

The number of arguments may vary, but we can still list all possible arities. To define definite variable arity function we can specify default argument values for all or some of the parameters.

`fun sum(a:Int, b:Int = 7, c:Int = 4) {    //...}`

To call above function we only need to provide value for a parameter, because other parameters have the default value. The minimum arity of the sum function is equal to the number of parameters without the default value (1) and maximum arity equal to a total number of function parameters (3).

`sum(1) // arity 1sum(1, 2) // arity 2sum(1, 2, 4) // arity 3`

It is variable because number of argument may varry from call to call, but it is also definite because there is upper limit of arguments that we can supply.

### Fixed & indefinite arity

We can’t define the function of fixed & indefinite arity. It makes no sense to allow function been called with an indefinite number of arguments yet force it to be called with the same specific number of arguments.

Method overloading is a good example that utilizes arity in interesting way.

`Logger.log(message) // arity 1Logger.log(message, error) // arity 2`

These two functions are actually two different functions (despite having the same name) because they have a different arity.

### Summary

If you work with another language such as Python or Ruby, having two functions with the same name could be quite confusing. After writing few function with different arity’s you’ll notice that arity is quite a nice aspect of language and allows to write programs in a quite convenient way.

To learn about other concept and conductus take a look at Code building blocks series.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.