Is there a way that a function can remember its previous output and use that value during the next call to the function? For instance, assume there is a function, runningTotal
with a single argument x
that returns x
on the first call to runningTotal
but x + prevOutput
for every call after that. Is there a way to write such a function in python?
I am aware that this could be easily achieved by using a global variable in the function or by saving the previous value to a new variable, but I would like to avoid these solutions if possible. The reason I'm looking for an alternate solution is because this is one function in a program I'm working on with other people and I would like to avoid having to create more global variables than already established.