In logic, mathematics, and computer science, arity (/ˈærɪti/ (listen)) is the number of arguments or operands taken by a function, operation or relation. In mathematics, arity may also be called rank,^{[1]}^{[2]} but this word can have many other meanings. In logic and philosophy, arity may also be called adicity and degree.^{[3]}^{[4]} In linguistics, it is usually named valency.^{[5]}
In general, functions or operators with a given arity follow the naming conventions of n-based numeral systems, such as binary and hexadecimal. A Latin prefix is combined with the -ary suffix. For example:
A constant can be considered an operation of arity 0, called a nullary.
Also, outside of functional programming, a function without arguments can be meaningful and not necessarily constant (due to side effects). Such functions may have some hidden input, such as global variables or the whole state of the system (time, free memory, etc.).
Examples of unary operators in mathematics and in programming include the unary minus and plus, the increment and decrement operators in C-style languages (not in logical languages), and the successor, factorial, reciprocal, floor, ceiling, fractional part, sign, absolute value, square root (the principal square root), complex conjugate (unary of "one" complex number, that however has two parts at a lower level of abstraction), and norm functions in mathematics. In programming the two's complement, address reference, and the logical NOT operators are examples of unary operators.
All functions in lambda calculus and in some functional programming languages (especially those descended from ML) are technically unary, but see n-ary below.
According to Quine, the Latin distributives being singuli, bini, terni, and so forth, the term "singulary" is the correct adjective, rather than "unary".^{[6]} Abraham Robinson follows Quine's usage.^{[7]}
In philosophy, the adjective monadic is sometimes used to describe a one-place relation such as 'is square-shaped' as opposed to a two-place relation such as 'is the sister of'.
Most operators encountered in programming and mathematics are of the binary form. For both programming and mathematics, these include the multiplication operator, the radix operator, the often omitted exponentiation operator, the logarithm operator, the addition operator, and the division operator. Logical predicates such as OR, XOR, AND, IMP are typically used as binary operators with two distinct operands. In CISC architectures, it is common to have two source operands (and store result in one of them).
The computer programming language C and its various descendants (including C++, C#, Java, Julia, Perl, and others) provide the ternary conditional operator ?:
. The first operand (the condition) is evaluated, and if it is true, the result of the entire expression is the value of the second operand, otherwise it is the value of the third operand. The Python language has a ternary conditional expression, x if C else y
.
The Forth language also contains a ternary operator, */
, which multiplies the first two (one-cell) numbers, dividing by the third, with the intermediate result being a double cell number. This is used when the intermediate result would overflow a single cell.
The Unix dc calculator has several ternary operators, such as |
, which will pop three values from the stack and efficiently compute with arbitrary precision.
Many (RISC) assembly language instructions are ternary (as opposed to only two operands specified in CISC); or higher, such as MOV %AX, (%BX, %CX)
, which will load (MOV) into register AX the contents of a calculated memory location that is the sum (parenthesis) of the registers BX and CX.
From a mathematical point of view, a function of n arguments can always be considered as a function of a single argument that is an element of some product space. However, it may be convenient for notation to consider n-ary functions, as for example multilinear maps (which are not linear maps on the product space, if n ≠ 1).
The same is true for programming languages, where functions taking several arguments could always be defined as functions taking a single argument of some composite type such as a tuple, or in languages with higher-order functions, by currying.
In computer science, a function that accepts a variable number of arguments is called variadic. In logic and philosophy, predicates or relations accepting a variable number of arguments are called multigrade, anadic, or variably polyadic.^{[8]}
Latinate names are commonly used for specific arities, primarily based on Latin distributive numbers meaning "in group of n", though some are based on Latin cardinal numbers or ordinal numbers. For example, 1-ary is based on cardinal unus, rather than from distributive singulī that would result in singulary.
x-ary | Arity (Latin based) | Adicity (Greek based) | Example in mathematics | Example in computer science |
---|---|---|---|---|
0-ary | Nullary (from nūllus) | Niladic | A constant | A function without arguments, True, False |
1-ary | Unary | Monadic | Additive inverse | Logical NOT operator |
2-ary | Binary | Dyadic | Addition | OR, XOR, AND |
3-ary | Ternary | Triadic | Triple product of vectors | Conditional operator |
4-ary | Quaternary | Tetradic | ||
5-ary | Quinary | Pentadic | ||
6-ary | Senary | Hexadic | ||
7-ary | Septenary | Hebdomadic | ||
8-ary | Octonary | Ogdoadic | ||
9-ary | Novenary (alt. nonary) | Enneadic | ||
10-ary | Denary (alt. decenary) | Decadic | ||
More than 2-ary | Multary and multiary | Polyadic | ||
Varying | Variadic | Sum; e.g., | Variadic function, reduce |
n-ary means n operands (or parameters), but is often used as a synonym of "polyadic".
These words are often used to describe anything related to that number (e.g., undenary chess is a chess variant with an 11×11 board, or the Millenary Petition of 1603).
The arity of a relation (or predicate) is the dimension of the domain in the corresponding Cartesian product. (A function of arity n thus has arity n+1 considered as a relation.)
In computer programming, there is often a syntactical distinction between operators and functions; syntactical operators usually have arity 0, 1, or 2 (the ternary operator ?: is also common). Functions vary widely in the number of arguments, though large numbers can become unwieldy. Some programming languages also offer support for variadic functions, i.e., functions syntactically accepting a variable number of arguments.