pure functions | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

pure functions

Is it correct to say: "A function is pure if it doesn't change the state/value of any variable external to it.", meaning it only operates on variables whose scope doesn't exceed the function's body. Btw I don't the difference between state and value of a variable or if there is one, hence the / above.

27th Jun 2019, 10:02 AM
Ben Opp
2 Answers
+ 7
Ben's definition is right: A pure function is supposed to not interact with the outside world at all, except via arguments and return value. That means you can't open files and can't generate a random number and can't even print out stuff, because all of it will need interaction with the operating system.
27th Jun 2019, 10:57 AM
HonFu
HonFu - avatar
+ 1
I did not fully get what you meant, but I would say: "Pure function is a function, that does not touch global namespace."
27th Jun 2019, 10:37 AM
Seb TheS
Seb TheS - avatar