Hubbry Logo
search
logo

Pure function

logo
Community Hub0 Subscribers
Write something...
Be the first to start a discussion here.
Be the first to start a discussion here.
See all
Pure function

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

The following examples of C++ functions are pure:

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 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.

The I/O monad is a programming idiom typically used to perform I/O in pure functional languages.

See all
User Avatar
No comments yet.