Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

Pure Functions: Does "No Side Effects" Imply "Always Same Output, Given Same Input"

Anjali Sharma answered on January 4, 2023 Popularity 1/10 Helpfulness 1/10

Contents


More Related Answers


Pure Functions: Does "No Side Effects" Imply "Always Same Output, Given Same Input"

0

Here are a few counterexamples that do not change the outer scope but are still considered impure:

function a() { return Date.now(); }

function b() { return window.globalMutableVar; }

function c() { return document.getElementById("myInput").value; }

function d() { return Math.random(); } (which admittedly does change the PRNG, but is not considered observable)

Accessing non-constant non-local variables is enough to be able to violate the second condition.

I always think of the two conditions for purity as complementary:

the result evaluation must not have effects on side state

the evaluation result must not be affected by side state

The term side effect only refers to the first, the function modifying the non-local state. 

However, sometimes read operations are considered as side effects as well: when they are operations and involve writing as well, even if their primary purpose is to access a value. 

Examples for that are generating a pseudo-random number that modifies the generator's internal state, reading from an input stream that advances the read position, or reading from an external sensor that involves a "take measurement" command.

Popularity 1/10 Helpfulness 1/10 Language javascript
Source: Grepper
Tags: javascript
Link to this answer
Share Copy Link
Contributed on Jan 04 2023
Anjali Sharma
0 Answers  Avg Quality 2/10


X

Continue with Google

By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
X
Grepper Account Login Required

Oops, You will need to install Grepper and log-in to perform this action.