Pure function

Pure function

In computer programming, a function may be described as pure 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, 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.

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.

Examples

Pure functions

* sin(x), returning the sine of a number "x"
* length(s), returning the size of a string "s"
* encrypt(d), running an encryption algorithm on a piece of data "d"

Impure functions

* A hypothetical function today() that returns the current day of the week is impure because 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 because each call potentially yields a different value. (This is because pseudorandom generators use and update a global "seed" state. If we modify it to take the seed as an argument, i.e. random(seed); then random becomes pure, because multiple calls with the same seed value return the same random number.)
* printf() is impure because it causes output to an I/O device as a side effect.

Pure expressions

Pure functions are required to construct pure expressions. Constant expressions are pure by definition. An expression consisting of a function subexpression applied to one or more argument subexpressions is pure if both these statements about the subexpressions hold:
# The function and argument subexpressions are pure expressions.
# The function subexpression yields a pure function.

Typically the function subexpression is simply a function identifier. Pure expressions are often referred to as being referentially transparent.

Evaluation of a given pure expression will yield the same result regardless of when or how many times evaluation occurs during program execution. This property is what makes it meaningful to talk about an expressions "value". It also makes it possible to replace an expression with the corresponding value (or it with an equivalent alternative expression) without changing the meaning of a program.

Impure functions in pure expressions

The definitions above still allow some laxity with regard to purity. It is possible for a pure expression to yield an impure function (or more generally a value which contains one or more impure functions).

It is also possible for an expression to be pure even if one or more of the argument subexpressions yields an impure function (or a value which contains one or more impure functions). In this case the impure function(s) in the argument must not be applied during evaluation (but may be incorporated in the result somehow). However, dealing with programs that allow impure and pure functions to be mixed like this can be quite difficult in practice, thus purely functional programming languages do not allow impure functions to be defined.

See also

* Purely functional
* Referential transparency (computer science)
* Lambda calculus
* Side Effect


Wikimedia Foundation. 2010.

Игры ⚽ Нужна курсовая?

Look at other dictionaries:

  • Pure — may refer to: * A pure function * Pure Software, a company founded in 1991 by Reed Hastings to support the Purify tool * Pure FTPd, FTP server software * Pure Digital, a UK consumer electronics company specialising in DAB radios. * Pure… …   Wikipedia

  • Pure tone audiometry — (PTA) is the key hearing test used to identify hearing threshold levels of an individual, enabling determination of the degree, type and configuration of a hearing loss. Thus, providing the basis for diagnosis and management. PTA is a subjective …   Wikipedia

  • Pure sociology — is a controversial but revolutionary fact|date=July 2008 approach developed by Donald Black as an alternative to the individualistic focus of virtually all previous theories and paradigms in the discipline. Initially developed to explain… …   Wikipedia

  • Pure red cell aplasia — Classification and external resources ICD 10 D60 ICD 9 284.8 …   Wikipedia

  • Pure Pwnage — infobox television show name=Pure Pwnage bgcolour=#A4232A caption= Pure Pwnage title screen format=Mockumentary picture format= runtime=ca. 10–50 minutes creator=Geoff Lapaire, Jarett Cale starring= country=Canada network= first aired=May 11,… …   Wikipedia

  • Function (mathematics) — f(x) redirects here. For the band, see f(x) (band). Graph of example function, In mathematics, a function associates one quantity, the a …   Wikipedia

  • Pure mathematics — Broadly speaking, pure mathematics is mathematics motivated entirely for reasons other than application. It is distinguished by its rigour, abstraction and beauty. From the eighteenth century onwards, this was a recognized category of… …   Wikipedia

  • Function pointer — A function pointer is a type of pointer in C, C++, D, and other C like programming languages, and Fortran 2003.[1] When dereferenced, a function pointer can be used to invoke a function and pass it arguments just like a normal function. In… …   Wikipedia

  • Compile time function execution — (or compile time function evaluation, CTFE) is the ability of a compiler, that would normally compile a function to machine code and execute it at run time, to execute the function at compile time. This is possible if the arguments to the… …   Wikipedia

  • Critique of Pure Reason — Part of a series on Immanuel …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”