0

I have been playing around with functions and recursion and I don't understand why this would output None. I expected it to output [1,1,1,1,1,1,1,1,1,1,1] but even though there is an explicit return function it outputs None.

list = []
def func(x):
    x.append(1)
    if len(x) >10:
        return x
    else:
        func(x)

print (func(list))

Output:

None
  • the `else` is redundant in a recursive function. Note that this is not a recursive function because it's not returning a function in any case. – XxJames07- Sep 17 '22 at 14:45
  • You need to `return func(x)` in the recursive case. This is a common FAQ. – tripleee Sep 17 '22 at 14:48

1 Answers1

0
list_a = []
def func(x):
    x.append(1)
    if len(x) > 10:
        return x
    else:
        func(x)

func(list_a)
print (list_a)

output:

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]