Skip to main content
edited title
Link
m0meni
  • 803
  • 1
  • 7
  • 13

What do you call a function that's pure, meaningwhere the same input will always return the same output, but also has side effects?

Tweeted twitter.com/StackProgrammer/status/728158747542081536
Question Protected by gnat
Source Link
m0meni
  • 803
  • 1
  • 7
  • 13

What do you call a function that's pure, meaning the same input will always return the same output, but also has side effects?

Say we have a normal pure function such as

function add(a, b) {
  return a + b
}

And then we alter it such that it has a side effect

function add(a, b) {
  writeToDatabase(Math.random())
  return a + b;
}

It's not considered a pure function as far as I know because I often hear people call pure functions "functions without side effects." However, it does behave like a pure function as far as the fact that it will return the same output for the same inputs.

Is there a different name for this type of function, is it unnamed, or is it still actually pure and I'm mistaken about the definition of purity?