A post from another thread says that a function is said to be idempotent if it can be called multiple times without changing the result.
However the terms used (like no-side-effects and return-same-results) are relatively ambiguous. Consider this piece of code:
public class test {
int x = 0;
java.util.Random r = new java.util.Random();
public int F() {
return x + 1;
}
public void F2() {
x = r.nextInt();
}
}
Can we say that F()
is idempotent because successive calls to F()
returns the same value?
Or is it not idempotent since successive calls to F()
does not return the same value if F2()
is called inbetween?
PS: "idempotent" as defined in computer science, not maths.
Your function is not idempotent. It can return different results. Given the same input, an idempotent function always returns the same output.
More explicitly, if f() is idempotent (according to the computer science definition) then: