I'm not sure about universal definitions of purity, but from the point of view of Haskell (a language where programmers tend to care about things such as purity and referential transparency), only the first of your functions is "pure". The second version of add isn't pure. So in answer to your question, I'd call it "impure" ;)
According to this definition, a pure function is a function that:
- Only depends on its input. That is, given the same input, it will always return the same output.
- Is referentially transparent: the funcionfunction can be freely replaced by its value and the "behavior" of the program will not change.
With this definition, it's clear your second function cannot be considered pure, since it breaks rule 2. That is, the following two programs are NOT equivalent:
function f(a, b) {
return add(a, b) + add(a, b);
}
and
function g(a, b) {
c = add(a, b);
return c + c;
}
This is because even though both functions will return the same value, function f will write to the database twice but g will write once! It's very likely that writes to the database are part of the observable behavior of your program, in which case I've shown your second version of add isn't "pure".
If writes to the database aren't an observable part of your program's behavior, then both versions of add can be considered equivalent and pure. But I can't think of a scenario where writing to the database doesn't matter. Even logging matters!