Jump to content

Pure function

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Nbarth (talk | contribs) at 02:50, 19 June 2006. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

A function is pure if it returns the same output on the same input. This is a component of referential transparency.

Examples of pure functions

Arithmetic functions are pure, and they are the archetype.

Examples of impure functions

today() is impure, as at different times it will yield different results: it refers to some global state.

Similarly, any function that uses global state or a static variable is potentially impure.

random() is impure, as each call potentially yields a different value.