Pure function
≈ 2 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
In computer programming, a pure function is a function that has the following properties: # 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 # the function has no side effects (no mutation of non-local variables, mutable reference arguments or input/output streams).
In computer programming, a pure function is a function that has the following properties: 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 the function has no side effects (no mutation of non-local variables, mutable reference arguments or input/output streams). static int fact(int n) { return n std::string s = "Hello, world!"; int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int l = 0; for (int i = 0; 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: In Fortran and D, the pure keyword can be used to declare a function to be just side-effect free (i.e. have just the above property 2). The compiler may be able to deduce property 1 on top of the declaration. See also: . In the GCC, the pure attribute specifies property 2, while the const attribute specifies a truly pure function with both properties. Languages offering compile-time function execution may require functions to be pure, sometimes with the addition of some other constraints. Examples include constexpr of C++ (both properties). See also: .
Text derived from https://en.wikipedia.org/w/index.php?oldid=1366129273 under CC BY-SA 4.0; modifications have been made.