-3

I have two functions which I am unable to tell if they are pure or not.

Here is the first one.

someFunction(ref input1, ref input2) {
    input2 = input1 + input2
    return input2
}

I believe that its an impure function because it is allowing mutability of input2. But why is this a problem? We cannot access input2 outside of the function, so I don't see why it matters if it is mutated or not.

Here is my second function.

someFunction(ref input1, ref input2) {
     return input1 + input2
}

Again, I think this is an impure function because it's passing input1 and input2 by reference. But again, I don't see why this is a problem. We are not modifying input1 or input2... so why would this function be impure?

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
user6800688
  • 145
  • 3
  • 11
  • "We cannot access input2 outside of the function" What do you mean? – AndyG Sep 19 '16 at 18:16
  • Is this code intend to be C++ or not? This isn't even close to be compilable code. – Guillaume Racicot Sep 19 '16 at 18:16
  • 2
    If `ref` is supposed to signify "passing be reference", then why do you claim that "we cannot access input2 outside of the function"? In this case the argument passed to the function is synonymous with `input2`, meaning that whatever changes are made to `input2` will afftect that argument and will be visible outside the function. – AnT stands with Russia Sep 19 '16 at 18:17

1 Answers1

0

Look at Pure_Function.

Your first method has side effects (it changes input2). -> impure

The second one is pure, no side effects (and the other rules are not violated).

Jarod42
  • 203,559
  • 14
  • 181
  • 302