When a function - recursive or not - is called, what matters to the calling function is the return value of it. Not the functions she calls. For example:
def fatorial(n):
if n == 1:
return 1
return n * fatorial(n-1)
In this case, the last recursive call will return a value, but this value will only be used by the previous call to it . You need to pass this value (possibly modified, as in the example above) to the "above" function, until you get to the calling function.
Another way to write the fatorial
function above is by using a accumulator - which prevents new operations from being made after the recursive call, consisting of Cream Tour :
def fatorial(n, acc=1): # Acumula o valor 1 inicialmente
if n == 1:
return acc # Retorna o valor acumulado, não 1
return fatorial(n-1, acc * n) # Acumula o valor n no produtório
This was the same strategy you used, in this case amassing new_list
. But note that, even though it is a tail, it is necessary to use return
at the end, for call n
access n-1
, n-1
access n-2
, etc, until the calling function has access to the original function.
If you do not return anything, in Python this means that the return value will be None
. Then modify your last line to:
return cap_str_elems_r(ls[1:], new_list)