The question of whether this is a pure function is heavily dependent on definition and thus opinion-based. However, you have fortunately provided us with a definition from Wikipedia.
- 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 (usually—see below).
- 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 (usually—see below).
Given the same input, your fibn
will always produce the same output. That much is clear. And, as you say, the variables that are modified only exist within the local scope of the function, so there are no semantically observable side effects outside the function. The only thing that remains to be shown (a corollary to (1)) is that your function always terminates. There is a while
loop, but the proof that your while
loop will terminate given any finite n
is trivial. Therefore, based on the provided definition, your function is pure.
Now, I'm assuming that you're listing "must be an integer" as a precondition. If you do not list this as a requirement, then I could produce a class whose ordering methods always make it greater than any k
value, thereby forcing an infinite loop. And an infinite loop has a very observable side effect: your program tends not to terminate in that case. However, if you constrain your attention to integer inputs, your function is pure.