FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/Pure_function

Pure function

Contents
    Impure functions
I/O in pure functions
Unit testing
See also

In computer programming, a pure function is a function that has the following properties:

  1. the function return values are identical for identical arguments (no variation with local static variables, non-local variables, mutable reference arguments or input streams, i.e., referential transparency), and
  2. the function has no side effects (no mutation of non-local variables, mutable reference arguments or input/output streams).
voidf(){ staticstd::atomic<unsignedint>x=0; ++x; }
The value of x can be only observed inside other invocations of f(), and as f() does not communicate the value of x to its environment, it is indistinguishable from function void f() {} that does nothing. Note that x is std::atomic so that modifications from multiple threads executing f() concurrently do not result in a data race, which has undefined behavior in C and C++.

Impure functions

edit

The following C++ functions are impure as they lack the above property 1:

\nint f() {\n static int x = 0;\n ++x;\n return x;\n}\n</syntaxhighlight>"},"2":{"wt":"because of return value variation with a non-local variable\n<syntaxhighlight lang=\"c++\">\nint f() {\n return x;\n}\n</syntaxhighlight>\nFor the same reason, e.g. the C++ library function <code>sin()</code> is not pure, since its result depends on the [[IEEE rounding mode]] which can be changed at runtime."},"3":{"wt":"because of return value variation with a mutable reference argument\n <syntaxhighlight lang=\"c++\">\nint f(int* x) {\n return *x;\n}\n</syntaxhighlight>"},"4":{"wt":"because of return value variation with an input stream\n<syntaxhighlight lang=\"c++\">\nint f() {\n int x = 0;\n std::cin >> x;\n return x;\n}\n</syntaxhighlight>"}},"i":0}}]}'>

The following C++ functions are impure as they lack the above property 2:

\nvoid f() {\n static int x = 0;\n ++x;\n}\n</syntaxhighlight>"},"2":{"wt":"because of mutation of a non-local variable\n<syntaxhighlight lang=\"c++\">\nvoid f() {\n ++x;\n}\n</syntaxhighlight>"},"3":{"wt":"because of mutation of a mutable reference argument\n<syntaxhighlight lang=\"c++\">\nvoid f(int* x) {\n ++*x;\n}\n</syntaxhighlight>"},"4":{"wt":"because of mutation of an output stream\n<syntaxhighlight lang=\"c++\">\nvoid f() {\n std::cout << \"Hello, world!\" << std::endl;\n}\n</syntaxhighlight>"}},"i":0}}]}'>

I/O in pure functions

edit

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.std::strings="Hello, world!"; inta[10]={1,2,3,4,5,6,7,8,9,10}; intl=0; for(inti=0;i<10;++i){ l+=s.length()+a[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:

Unit testing

edit

Since pure functions have identical return values for identical arguments, they are well suited to unit testing.

See also

edit