Arity

Arity, valence or valency refers to the number of parameters that can be passed to a function, procedure or other operation. The most common arities are 0, 1, 2 and a variable arity.
== Examples ==
Consider the mathematical expression 10 + 20. The + operator uses two values, 10 and 20, so we say it has an arity of 2. In contrast, the - operator has two possible arities: arity 2, e.g. 4 - 3 and arity 1, -1.
Many languages have some concept of arity with respect to functions. The following Python-esque pseudocode defines three functions with different arities:
 def arity0():
 return 42
 
 def arity3(a, b, c):
 if a == 42:
 return b
 else:
 return c
 
 def variableArity(*some_arguments):
 number_of_args = length(some_arguments)
 if number_of_args == 3:
 a, b, c = some_arguments
 return arity3(a, b, c)
 else if number_of_args == 0:
 return arity0()
 else:
 return "???"
 
 x = arity0() # 42
 y = arity3(x, 100, 200) # 100
 z = variableArity() # 42
 questionmarks = variableArity(1, 2, 3, 4) # '???'
=== Practical uses ===
* Pyth functions all have a predefined arity, which is valuable for golfing - surround notation for delimiting lists of arguments would cost valuable characters that are not required in prefix notation.
* Clue functions can be defined so they have any number of parameters, but this number is fixed during execution.
* In APL, (almost) every function is either dyadic (there is a parenthesized expression or a nilad preceding it) or monadic (otherwise).
== Dynamic arity==
While some languages only allow any given function to have a single predefined arity, many offer leniency by allowing the programmer to define functions that can accept any non-negative integer amount of arguments to a function. To use non-esoteric languages as case studies, Lisp languages often allow conversion of the passed values into a single list of values, which can be manipulated as a single value. Python supports 'default arguments' which allows flexibility with how specific you want to be. For example, sum([1,2,3]) and sum([1,2,3], 12) are similar, except the latter sets the initial value of the summation to 12, whereas the former leaves it as the default 0.
=== Currying ===
A feature that permeates functional programming, Currying, uses only arity-1 functions, which can return another arity-1 function, ad infinitum, until the final function has enough parameters to perform a calculation. Syntactic sugar makes these look to the programmer like a single function with multiple parameters. Any multi-parameter function can be converted via Currying to a higher-order arity-1 function. add(2, 3) transforms to add(2)(3), where add(2) denotes a function that accepts a single argument and adds 2 to it. The overall expression runs that function on 3, resulting in 5, of course. This is a feature of higher-order programming and the lambda calculus in general, so esolangs derived from lambda calculus often rely on it.
== Nomenclature ==
There are many naming conventions for the various arities. The Greek-derived words for numbers use 'niladic', 'monadic', 'dyadic' and 'triadic' for arities 0 through 3, and the word 'polyadic' for operations of many parameters. The corresponding terms of Latin origin are 'nullary', 'unary', 'binary' and 'ternary' respectively, plus 'multary' for many parameters. For functions that can take any number of parameters, 'variadic' is the frequent descriptor in computer science, or, less commonly, 'varargs', short for 'variable amount of arguments'. The terms ending in '-ic' can be used as nouns by removing the suffix, such as a 'dyad' for an operation that acts on two values.
Category:Concepts