HOME

TheInfoList



OR:

In
computer programming Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, a pure function is a function that has the following properties: # the function return values are identical for identical
arguments An argument is a series of sentences, statements, or propositions some of which are called premises and one is the conclusion. The purpose of an argument is to give reasons for one's conclusion via justification, explanation, and/or persua ...
(no variation with local static variables, non-local variables, mutable reference arguments or input streams, i.e., referential transparency), and # the function has no side effects (no mutation of non-local variables, mutable reference arguments or input/output streams).


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 a 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.


Memoization

The outputs of a pure function can be cached in a look-up table. Any result that is returned from a given function is cached, and the next time the function is called with the same input parameters, the cached result is returned instead of computing the function again. Memoization can be performed by wrapping the function in another function ( wrapper function). By means of memoization, the computational effort involved in the computations of the function itself can be reduced, at the cost of the overhead for managing the cache and an increase of memory requirements. A C program for cached computation of
factorial In mathematics, the factorial of a non-negative denoted is the Product (mathematics), product of all positive integers less than or equal The factorial also equals the product of n with the next smaller factorial: \begin n! &= n \times ...
( assert() aborts with an error message if its argument is false; on a 32-bit machine, values beyond fact(12) cannot be represented.) static int fact(int n) int fact_wrapper(int n)


Compiler optimizations

Functions that have just the above property 2 – that is, have no side effects – 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. See also: . * 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> See also: .


Unit testing

Since pure functions have identical return values for identical
arguments An argument is a series of sentences, statements, or propositions some of which are called premises and one is the conclusion. The purpose of an argument is to give reasons for one's conclusion via justification, explanation, and/or persua ...
, they are well suited to
unit testing Unit testing, component or module testing, is a form of software testing by which isolated source code is tested to validate expected behavior. Unit testing describes tests that are run at the unit-level to contrast testing at the Integration ...
.


See also

* Compile-time function executionThe evaluation of pure functions at compile time * Deterministic algorithmAlgorithm that, given a particular input, will always produce the same output * IdempotenceProperty of operations whereby they can be applied multiple times without changing the result * * * Reentrancy (computing)Executing a function concurrently without interfering with other invocations


References

{{Reflist Functional programming Subroutines