3

I am learning resursion recently and I wrote a simple recursive function to verify my understanding:

def hello(n):
    if n == 1:
        return 'hello'
    else:
        print('hello')
        hello(n-1)

def returnhello():
    return 'hello'

print(returnhello())
print()
print(hello(5))

the output of this is shown here:

hello

hello
hello
hello
hello
None

Why the last call in the recursion prints None instead of hello? I was expecting it to print 5 hello

Hewen Chen
  • 63
  • 5
  • Duplicates: https://stackoverflow.com/questions/19215141/recursive-function-returning-none-in-python https://stackoverflow.com/questions/15210646/i-expect-true-but-get-none https://stackoverflow.com/questions/2599149/python-recursion-with-list-returns-none https://stackoverflow.com/questions/44061245/python-recursion-returning-none-value https://stackoverflow.com/questions/31739963/recursive-function-returning-none https://stackoverflow.com/questions/15340281/python-recursion-return-none-type https://stackoverflow.com/questions/36692143/why-python-recursion-return-none-value – Stef Sep 20 '21 at 09:59
  • @Stef please just vote to close questions. If you don't know which one is canonical, ask or discuss in the [Python chat room](https://chat.stackoverflow.com/rooms/6/python), or try https://sopython.com/canon. – Karl Knechtel Aug 12 '22 at 23:57

3 Answers3

5

This is because in your else part in hello(n) you don't have a return statement before hello(n-1), so the first call (exiting the last) will return a None.

If you put a return before hello(n-1) you should get what you want.

saedx1
  • 984
  • 6
  • 20
2

The right recursion function for your expected output is:

def hello(n):
    if n == 1:
        return 'hello'
    else:
        print('hello')
        return hello(n-1)

def returnhello():
    return 'hello'

print(returnhello())
print()
print(hello(5))

Also, it can be written as:

def hello(n):
    if n==1:
        print("hello")
    else:
        print("hello")
        hello(n-1)
        
def returnhello():
    return 'hello'

print(returnhello())
print()
hello(5)

The output will be:

hello

hello
hello
hello
hello
hello

Note:

You mustn't use print with a recursive function, you can use a function with return statement or without any statement.

Raha Moosavi
  • 527
  • 4
  • 19
1

@saedx has identified and corrected your issue. Python returns None by default and that is what you see printed after the function returns.

You could implement your hello function to be a bit more consistent in how it displays the strings. At present the first n-1 are printed in the body of the function but the caller is left to print the final one.

Here the function prints all n of the strings.

def hello(n):
    print('hello')
    if n > 1:
        hello(n-1)

hello(5)

In this case you just call the function. You don't print what it returns.

The other way would be to have the caller print all n of the strings.

def hello(n):
    yield 'hello'
    if n > 1:
        yield from hello(n-1)

Which is then called like

print('\n'.join(hello(5)))

Also note that both these examples remove the duplication of the string being printed. Its worth noting that if you passed in a number less than 1 you'd be in trouble, as it would recurse infinitely. So we could throw an exception in that case.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61