Can a pure function depend on an external constant?

Viewed 1527

An article I was reading gives this as an example of an impure function (in JavaScript):

const tipPercentage = 0.15;

const calculateTip = cost => cost * tipPercentage;

That struck me as a bit of an odd example, since tipPercentage is a constant with an immutable value. Common examples of pure functions allow dependence on immutable constants when those constants are functions.

const mul = (x, y) => x * y

const calculateTip = (cost, tipPercentage) => mul(cost, tipPercentage);

In the above example, correct me if I'm wrong, calculateTip would usually be categorised as a pure function.

So, my question is: In functional programming, is a function still considered pure if it relies on an externally defined constant with an immutable value, when that value is not a function?

1 Answers
Related