Is a function that changes the values of an input argument still a pure function?
My example (Kotlin):
data class Klicker( var id: Long = 0, var value: Int = 0 ) fun Klicker.increment() = this.value++ fun Klicker.decrement() = this.value-- fun Klicker.reset() { this.value = 0 } Wikipedia says a pure function has these two requirements:
- The function always evaluates the same result value given the same argument value(s). The function result value cannot depend on any hidden information or state that may change while program execution proceeds or between different executions of the program, nor can it depend on any external input from I/O devices.
- Evaluation of the result does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to I/O devices.
From my understanding, all functions from my example comply with the first requirement.
My uncertainty starts with the second requirement. With the change of the input argument, I mutate an object (rule violation), but this object is not outside of the function scope, so maybe no rule violation?
Also, does a pure function always need to return a completely new value?
I presume, this function is considert 100% pure:
fun pureIncrement(klicker: Klicker): Klicker { return klicker.copy(value = klicker.value++) } Be gentle, this is my first Stackoverflow question.
pureIncrementmutatesklicker.valuewith++, so it's not pure.