Just trying to understand what is and what isn't a side effect of a function in relation to functional programming. For example, if we have a very simple integer division function like this:
def integer_division(a, b, count=0):
if (a < 0) ^ (b < 0):
sign = -1
else:
sign = 1
if abs(a) - abs(b) == 0:
return (count + 1) * sign
elif abs(a) - abs(b) < 0:
return count * sign
else:
return integer_division((abs(a) - abs(b))*sign, b, count + 1)
if __name__ == '__main__':
print(integer_division(-5, 2))
Out: -2
Would sign variable be considered a side effect? On one hand it is only defined within the function, but then on the other hand it is entirely based on the function parameters and as such shouldn't be one. While I was pondering the question I re-wrote the function as this...:
def integer_division_diff(a, b, count=0, sign=1):
if (a < 0) ^ (b < 0):
sign *= -1
if abs(a) - abs(b) == 0:
return (count + 1) * sign
elif abs(a) - abs(b) < 0:
return count * sign
else:
return integer_division((abs(a) - abs(b)) * sign, b, count + 1)
...which does exactly the same job and produces the same result, but which one is a more "correct" from a functional programming perspective?
EDIT: fixed a glaring issue when a is a multiple of b.