1

We have the notion of a pure function, whose return value depends only and entirely on its parameters, and which does not change global state.

Example:

def f(a, b):
    return a+b

What would be the correct term for a function that only meets the latter criterion? For example, a function (in a garbage collected language) that allocates a new mutable structure on each call, such that the return value is different for identical parameters?

Example:

def f(a, b):
    return [a, b]

The importance of this classification, from my perspective, is that it is significant to an optimizer: if a function is of this kind, then calls to it can be deleted whenever the return value is not being used. The most concise description I know of is 'calls to this function have no side effects', but it seems like there should be a more concise term.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
rwallace
  • 31,405
  • 40
  • 123
  • 242
  • 1
    Fun fact: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html GCC uses `__attribute__((const))` for truly pure functions, that don't even read global state, only their args. It uses `__attribute__((pure))` for what you're talking about: ones with no side effects, such that they can be [CSEd](https://en.wikipedia.org/wiki/Common_subexpression_elimination) as long as no globally-visible variables have been written between calls. I'm guessing that `pure` was defined first, forcing them to make up a different name for the stronger promise of actual purity. – Peter Cordes Dec 15 '22 at 16:44

0 Answers0