r/functionalprogramming • u/Echoes1996 • Dec 02 '24
Question Is this function pure?
Consider a function f(x) that invokes an impure function g(), which is not referentially transparent, though it has no side effects. Then, function f decides to do nothing with the result returned by function g and goes on to return a value based on its argument x. My question is: is f pure?
Example:
global y
def g():
# Not referentially transparent, though it does not
# alter the "outside world".
return y
def f(x: int):
_ = g() # Invoke non-referentially transparent function g.
return x + 1 # Return result solely based on input x.The output of f is completely dependent on its input, and it has no side effects, as g has no side effects as either. So, is f pure or not?
7
Upvotes
View all comments
Show parent comment
3
u/Longjumping_Quail_40 Dec 02 '24
For example, since merely from the code of
gone is not able to deducexis even well defined. It is only when you look at the bigger context then you have this information. This is a side effect.