Pure functions can change input values?

I'm studying functional programming and I just read about Pure functions.

My question is: A pure function can change its parameters?

Is this function pure?

int a(Payment payment){
 payment.setValue(payment.getValue() - 1);
 return payment.getValue() * 10;
}

Upvotes: 0

Views: 2028

Answers (2)

Sylwester
Sylwester

Reputation: 48755

The rule is no side effects. Mutating an object is a side effect. However it doesn't really say that you are mutating so here is a version of Payment where your code is pure:

public class Payment
{
  private int value;

  public Payment(int value) {
    this.value = value;
  }
  public Payment setValue(int value) {
    return new Payment(value);
  }
  public int getValue() {
    return value;
  }

  public static int a(Payment payment) {
      payment.setValue(payment.getValue() - 1); // dead code
      return payment.getValue() * 10;
  }

  public static int b(Payment payment) {
      return payment
        .setValue(payment.getValue() - 1)
        .getValue() * 10;
  }

  public static void main(String args[]) {
      Payment p = new Payment(10);
      System.out.println(String.valueOf(a(p)));
      System.out.println(String.valueOf(b(p)));
  }
}

Notice the definition of your a isn't changed and it is 100% functional. If you pass it 10 it returns 100, always 10 times the input. However the first line that returns a new Payment with the value 9 is never used so it is dead code. The intent was perhaps that of my rewritten version, b. b is also functional.

Now other implementations of Payment might make a and b impure.

Many people think OO and functional doesn't mix. One of the oldest and most commonly used class in Java, java.lang.String, is both object oriented and a purely functional interface. No method mutates, only return a new object.

Upvotes: 2

PiotrK
PiotrK

Reputation: 1552

Two things here:

  1. A pure function is a function with return value based only on its input arguments, and calling the function has no side effects.

  2. The piece of Kotlin code you pasted doesn't compile (see https://ideone.com/fli05T). The compiler says val cannot be reassigned. It's because in Kotlin, function arguments are immutable.

In theory, if it was possible to define such function as you wrote, then by definition from (1), it would be pure, assuming that the parameters are passed by value (changing them inside the function doesn't change them as seen from outside the function).

Upvotes: 3

Related Questions