So, I believe I understand pure functions. Something similar to abs or sqrt where the output is dependant on input and has no side effects. However I am confused about how this works with methods.
If we view a method as a function with an implicit this parameter, then I would assume that a method such as the one below would indeed be pure.
class Foo
{
int x;
[Pure] public int Bar() { return x * 2; }
}
Is it a correct assumption that the function is pure? Would it make a difference if the read variable was readonly/const?
A function is considered pure when:
Bar
isn't pure because it depends on the variablex
. So the result ofBar()
would be different on different occasions if the value ofx
changes.Imagine something like this:
Whereas, if
x
were a constant/readonly, it would still be pure because, the results ofBar()
would never change.