StackOverflow pointed me here, so the question might be a bit in a layman's terms.
Wikipedia defines pure functions as
In computer programming, a function may be described as a pure function if both these 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 as program execution proceeds or between different executions of the program, nor can it depend on any external input from I/O devices.
- 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.
However it does not seem to cite any sources -- so it is hard to say whether this is an accepted definition, or who defined it this way.
When I look at what the languages do when they include a syntax/annotation for "pure" functions, there are quite a few different approaches:
- In D the only limitation is non-mutation of global state. "Pure" functions can mutate its arguments.
- In GCC there are two types of "pure":
pure
(no side-effects, but can read global state) andconst
(stricly pure as per wikipedia definition). - In C#, it is defined as "does not make any visible state changes" (whatever that is).
- Haskell follows the Wikipedia definition.
So my question is: is there a canonical definition of pure function?
And if there is, what is its source?