0

I am new to recursion with Python and was trying to learn about linked-lists. The resource that I got the code below is http://www.openbookproject.net/thinkcs/python/english2e/ch18.html I was wondering why the return statement in line 2 doesn't need any arguments. I have tested that the code works by passing a linked-list to the function. Thank you!

def print_backward(list):
    if list == None: return
    head = list
    tail = list.next
    print_backward(tail)
    print head,
4
  • 1
    A return with no arguments is implicitly return None. Also, this return is not meant to return anything in the first place, it's just to break the recursion. That function does not return anything, but it prints. Commented Dec 27, 2014 at 18:49
  • Thanks, tobias_k. If that is the case, when does the function ever get to the "print head," statement? To my understanding, the print_backward(tail) statement above it is making recursive calls which prevent it from executing before we reach the "return" line. Commented Dec 27, 2014 at 18:57
  • See this post to understand better how recursion works: stackoverflow.com/questions/27555189/… Commented Dec 27, 2014 at 18:58
  • @Kidus A, one more thing although not related to recursion, you'd better not use list as a name, it's a shadow name of python built-in function list. Commented Dec 27, 2014 at 19:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.