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
The following C++ functions are impure as they lack the above property 1:
because of return value variation with a static variable
intf(){staticintx=0;++x;returnx;}
because of return value variation with a non-local variable
intf(){returnx;}
For the same reason, e.g. the C++ library function sin() is not pure, since its result depends on the IEEE rounding mode which can be changed at runtime.
because of return value variation with a mutable reference argument
intf(int*x){return*x;}
because of return value variation with an input stream
intf(){intx=0;std::cin>>x;returnx;}
The following C++ functions are impure as they lack the above property 2:
because of mutation of a local static variable
voidf(){staticintx=0;++x;}
because of mutation of a non-local variable
voidf(){++x;}
because of mutation of a mutable reference argument
voidf(int*x){++*x;}
because of mutation of an output stream
voidf(){std::cout<<"Hello, world!"<<std::endl;}
The following C++ functions are impure as they lack both the above properties 1 and 2:
because of return value variation with a local static variable and mutation of a local static variable
intf(){staticintx=0;++x;returnx;}
because of return value variation with an input stream and mutation of an input stream
intf(){intx=0;std::cin>>x;returnx;}
I/O in pure functions
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.[clarification needed]
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.[3][4]
The outputs of a pure function can be precomputed and cached in a look-up table. In a technique called memoization, any result that is returned from a given function is cached, and the next time the function is called with the same input parameters, the cached result is returned instead of computing the function again.
Memoization can be performed by wrapping the function in another function (wrapper function).[5]
By means of memoization, the computational effort involved in the computations of the function itself can be reduced, at the cost of the overhead for managing the cache and an increase of memory requirements.
A C program for cached computation of factorial (assert() aborts with an error message if its argument is false; on a 32-bit machine, values beyond fact(12) cannot be represented anyway.[citation needed]
Functions that have just the above property 2 – that is, have no side effects – allow for compiler optimization techniques such as common subexpression elimination and loop optimization similar to arithmetic operators.[6] A C++ example is the length method, returning the size of a string, which depends on the memory contents where the string points to, therefore lacking the above property 1. Nevertheless, in a single-threaded environment, the following C++ code
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).[7] The compiler may be able to deduce property 1 on top of the declaration.[8] See also: Fortran 95 language features § Pure procedures.
In the GCC, the pure attribute specifies property 2, while the const attribute specifies a truly pure function with both properties.[9]
^Hanus, Michael. "Curry: An Integrated Functional Logic Language"(PDF). www-ps.informatik.uni-kiel.de. Institut für Informatik, Christian-Albrechts-Universität zu Kiel. p. 33. Archived from the original(PDF) on 25 July 2014. Retrieved 17 July 2014.