Pure function
From Wiki @ Karl Jones dot com
Revision as of 16:08, 24 September 2016 by Karl Jones (Talk | contribs)
In computer programming, a function may be considered a pure function if both of the following statements about the function hold:
- The function always evaluates the same result value given the same argument value(s). The function result value cannot depend on any hidden information or state that may change while program execution proceeds or between different executions of the program, nor can it depend on any external input from I/O devices (usually—see below).
- Evaluation of the result does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to I/O devices (usually—see below).
The result value need not depend on all (or any) of the argument values. However, it must depend on nothing other than the argument values. The function may return multiple result values and these conditions must apply to all returned values for the function to be considered pure. If an argument is call by reference, any parameter mutation will alter the value of the argument outside the function, which will render the function impure.
See also
- Compile time function execution - the evaluation of pure functions at compile time
- Purely functional data structure
- Referential transparency (computer science)
- Lambda calculus
- Side effect (computer science)
- Pure procedure
- Idempotence
- pure keyword in Fortran annotating pure functions
- constexpr keyword in C++ annotating pure functions usable at compile-time
External links
- Pure function @ Wikipedia