0

Recursive variable without using global?

Hello, so I have made a recursive function that returns a sum of all negative numbers in a list as what I wrote here: https://code.sololearn.com/cfhhP5DEV3AU/?ref=app is there a way to not use global in this function?

29th Sep 2019, 2:42 PM
Steven Sim
Steven Sim - avatar
2 Answers
+ 2
Pass "result" as an argument to your function. def addall(x, result=0): if len(x) == 0: print(result) else: if x[0] < 0: result += x[0] addall(x[1:], result) else: addall(x[1:], result)
29th Sep 2019, 2:52 PM
Diego
Diego - avatar
+ 1
Diego Thank you
29th Sep 2019, 2:58 PM
Steven Sim
Steven Sim - avatar