In computer programming, a pure function is a function that has the following properties:[1][2] Show
Thus a pure function is a computational analogue of a mathematical function. Some authors, particularly from the imperative language community, use the term "pure" for all functions that just have the above property 2[3][4] (discussed below). Examples[edit]Pure functions[edit]The following examples of C++ functions are pure:
Impure functions[edit]The following C++ functions are impure as they lack the above property 1:
The following C++ functions are impure as they lack the above property 2:
The following C++ functions are impure as they lack both the above properties 1 and 2:
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 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. The second point ensures that the only sequence usable as an argument must change with each I/O action; the first allows different calls to an I/O-performing function to return different results on account of the sequence arguments having changed.[5][6] The I/O monad is a programming idiom typically used to perform I/O in pure functional languages. Compiler optimizations[edit]Functions that have just the above
property 2 allow for compiler optimization techniques such as common subexpression elimination and loop optimization similar to arithmetic
operators.[3] A C++ example is the 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 < 10; ++i) { l += s.length() + a[i]; } can be optimized such that the value of 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]
References[edit]
What type of function produces the same output value every time it is called using the same parameters?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), and.
What do you call a function where the same input will always return the same output?A pure function is a function that will always give the same output with the same input and has no side effects. A function that takes some arguments will always return the same value and not be affected by variables outside the function, nor will it change variables outside of it after it's done executing.
Which of the following model will give the same output given the same input?A deterministic algorithm will always produce the same output given a certain input.
What is a function side effect?A side effect is when a function relies on, or modifies, something outside its parameters to do something. For example, a function which reads or writes from a variable outside its own arguments, a database, a file, or the console can be described as having side effects.
|