Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a pure function use a private constant variable inside the same class?

Can a pure function use a private constant variable inside the same class?

for example:

class TimesThousand {
  const CONSTANT = 1000;

  function calculate(number) {
    return number * CONSTANT;
  }
}

can calculate() be considered as a pure function?

like image 922
bbnn Avatar asked May 10 '26 07:05

bbnn


1 Answers

A pure function is pure, when the return value is only determined by its input values, without any observable side effects.

So your function is pure. Since the value of CONSTANT is (as the name suggests) constant, the output is purely determined by the input.

From Wikipedia:

a function may be considered a pure function if both of the following statements about the function hold:

  1. 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.
  2. 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.
like image 85
Luka Jacobowitz Avatar answered May 14 '26 08:05

Luka Jacobowitz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!