In
computer programming
Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as anal ...
, a pure function is a
function that has the following properties:
# the function
return values are
identical for identical
arguments (no variation with local
static variables,
non-local variable
In programming language theory, a non-local variable is a variable that is not defined in the local scope. While the term can refer to global variables, it is primarily used in the context of nested and anonymous functions where some variables can ...
s, mutable
reference arguments or
input streams), and
# the function has no
side effects (no mutation of local static variables, non-local variables, mutable reference arguments or input/output streams).
Thus a pure function is a computational analogue of a
mathematical function. Some authors, particularly from the imperative language community, use the term "pure" for all functions that just have the above property 2
(discussed
below).
Examples
Pure functions
The following examples of
C++ functions are pure:
Impure functions
The following C++ functions are impure as they lack the above property 1:
The following C++ functions are impure as they lack the above property 2:
The following C++ functions are impure as they lack both the above properties 1 and 2:
I/O in pure functions
I/O is inherently impure: input operations undermine
referential transparency, and output operations create side effects. Nevertheless, there is a sense in which function can perform input or output and still be pure, if the sequence of operations on the relevant I/O devices is modeled explicitly as both an argument and a result, and I/O operations are taken to fail when the input sequence does not describe the operations actually taken since the program began execution.
The second point ensures that the only sequence usable as an argument must change with each I/O action; the first allows different calls to an I/O-performing function to return different results on account of the sequence arguments having changed.
The
I/O monad is a
programming idiom typically used to perform I/O in pure functional languages.
Compiler optimizations
Functions that have just the above property 2 allow for compiler optimization techniques such as
common subexpression elimination and
loop optimization similar to arithmetic operators.
A C++ example is the
length
method, returning the size of a string, which depends on the memory contents where the string points to, therefore lacking the above property 1. Nevertheless, in a
single-threaded environment, the following C++ code
std::string s = "Hello, world!";
int a 0= ;
int l = 0;
for (int i = 0; i < 10; ++i)
can be optimized such that the value of
s.length()
is computed only once, before the loop.
Some programming languages allow for declaring a pure property to a function:
* In
Fortran and D, the
pure
keyword can be used to declare a function to be just side-effect free (i.e. have just the above property 2). The compiler may be able to deduce property 1 on top of the declaration.
* In the
GCC, the
pure
attribute specifies property 2, while the
const
attribute specifies a truly pure function with both properties.
* Languages offering
compile-time function execution may require functions to be pure, sometimes with the addition of some other constraints. Examples include
constexpr
of C++ (both properties).
constexpr attribute in C++
/ref>
Unit testing
Since pure functions have identical return values for identical arguments, they are well suited to unit testing.
See also
* Compile-time function execution: the evaluation of pure functions at compile time
* Deterministic algorithm
In computer science, a deterministic algorithm is an algorithm that, given a particular input, will always produce the same output, with the underlying machine always passing through the same sequence of states. Deterministic algorithms are by far ...
* Purely functional data structure
* Lambda calculus
Lambda calculus (also written as ''λ''-calculus) is a formal system in mathematical logic for expressing computation based on function abstraction and application using variable binding and substitution. It is a universal model of computation ...
* Side effect (computer science)
In computer science, an operation, function or expression is said to have a side effect if it modifies some state variable value(s) outside its local environment, which is to say if it has any observable effect other than its primary effect of ...
* Pure procedure
* Idempotence
Idempotence (, ) is the property of certain operations in mathematics and computer science whereby they can be applied multiple times without changing the result beyond the initial application. The concept of idempotence arises in a number of pla ...
* pure keyword in Fortran annotating pure functions
* constexpr
C++11 is a version of the ISO/ IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called C++03, and was later replaced by C++14. The name follows the tradition of naming language versio ...
keyword in C++ annotating pure functions usable at compile-time
References
{{Reflist
Functional programming
Subroutines