When a function - recursive or not - is called, what matters to the calling function is its return value. Not of the functions it 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 previous call to her. You need to pass this value (possibly modified, as in the example above) to the function "above" it, until you reach the calling function.
Another way to write the function fatorial
above is using a accumulator - which prevents new operations from being made after the recursive call, thus consisting of a tail recursion:
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
That was the same strategy you used, in the case accumulating new_list
. But note that, even being tail, it is necessary to use the return
at the end, to call n
have access to n-1
, to n-1
have access to 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)
It’s because when you don’t
else
it returns nothing. Change the last line toreturn cap_str_elems_r(ls[1:], new_list)
– mgibsonbr
Yes, but what is returned is the return value of the first call (not the last). P.S. I will explain better in a reply.
– mgibsonbr