There are at least five issues with your code:
- The call to
solution(arr, sum, i) in calcsum does not modify calcsum's local variable sum, thus calcsum always returns 0;
- You wrote
return instead of return sum in the end case for the recursion in solution, so solution will return None instead of returning the sum in that case
- You wrote the recursive call as
solution(arr,sum,i+1) instead of return solution(arr,sum,i+1) or some_variable = solution(arr,sum,i+1), so the return value of the recursive call is ignored anyway; there is no return statement except the one inside the if block, so the function solution will not have a return value (or more precisely, it will return None);
- You seem to believe that modifying
sum inside solution will modify any variable called sum throughout your program. This is wrong. sum is a local variable of the function, and modifying it has no impact on the rest of the program.
- Calling a variable
sum is a bad idea in python; this is the name of a builtin function, so you should try to find another name for your variables.
To illustrate the fourth point, try out this code:
def mostly_harmless(n):
n = 7
n = 8
mostly_harmless(n)
print('Does this set n to 7? Am I about to print 8 or 7?')
print(n)
print()
mostly_harmless(12)
print('did we set 12 = 7? did we just break all of mathematics?')
print('making sure 12 is still 12:')
print(12)
print('making sure n is still n:')
print(n)
With this in mind, we can fix your code:
def calcsum(arr):
return solution(arr, 0, 0)
def solution(arr, sum, i):
if i == len(arr):
return sum
else:
return solution(arr, sum + arr[i], i+1)
print(calcsum([1,2,3,4,5]))
Actually, we can simplify the code further using default arguments:
def calcsum(arr, sum=0, i=0):
if i == len(arr):
return sum
else:
return calcsum(arr, sum + arr[i], i+1)
print(calcsum([1,2,3,4,5]))
But note that python is not a language that likes recursion. In some other programming languages, recursion is very good, and exactly as efficient as for loops and while loops. But that is not the case in python. In python, recursion is much slower than loops, and uses much more memory. We can rewrite your program with a for loop:
def calcsum(arr):
sum = 0
for i in range(len(arr)):
sum += arr[i]
return sum
print(calcsum([1,2,3,4,5]))
Or even better:
def calcsum(arr):
sum = 0
for v in arr:
sum += v
return sum
print(calcsum([1,2,3,4,5]))
Also note that calling a variable sum in python is a bad idea, because sum is the name of a builtin function in python. You can find a list of builtins in the python documentation; try to give other names to your variables.
The function sum, unsurprisingly, calculates the sum all by itself:
print(sum([1,2,3,4,5]))
So why was it so bad to call a variable sum? Well, then we lose access to the builtin function with the same name:
sum = 7
print(sum([1,2,3,4,5]))
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# TypeError: 'int' object is not callable
Normally sum refers to the builtin function; but now it refers to a variable equal to 7; when I try to call sum([1,2,3,4,5]), it's as if I tried to call 7([1,2,3,4,5]), which doesn't make sense because 7 is not a function.