Pure functions and three sided triangles
Posted on
In recent years functional programming has become if not trendy then at least less niche. Well-meaning introductory articles often bring up the idea of 'pure functions'. The qualifier 'pure' suggests that pure functions are to be singled out in contrast to some other sort of function. What is it that sets pure functions apart? The answer varies across authors, but it tends to be something along these lines:
- Pure functions are deterministic in the sense that f(x)=y and f(x)=z implies that y=z.
- Pure function do not mutate their arguments.
- Pure functions have no side effects, say incrementing a global variable, writing to a log, or launching nuclear missiles.
- Pure functions are referentially transparent in the sense that if f(x)=y then we can substitute y for any occurrence of f(x) without change of meaning.
- Pure functions depend only on their arguments and immutable constants such as numbers.
These are all very nice, but there is no need for the 'pure' qualifier. Anything purporting to be a function but lacking these properties is just not a function. One could call such a thing a subroutine, a procedure, a method, a code blob, whatever. The word function already has a well established mathematical meaning that covers these purity concerns. It has been in use since Liebniz introduced it in 1673. The idea of a function has been around even longer.
To speak of 'pure' functions makes as much sense as speaking of 'three-sided' triangles or 'aqueous' water. There is no other kind. The fact that various programming languages misappropriate the word is unfortunate. I prefer the established mathematical usage.