3

So I was making a diagram of the recursive function to wrap my head around recursion, and I noticed that apparently every function executes return at the end?

Another question, what exactly does a function return? All parameters passed to it (assuming multiple parameters)? Or some kind of value?

(t is just an entity that performs actual drawing)

def koch(t, n):
    """Draws a koch curve with length n."""
    if n<30:
        fd(t, n)
        return
    m = n/3.0
    koch(t, m)
    lt(t, 60)
    koch(t, m)
    rt(t, 120)
    koch(t, m)
    lt(t, 60)
    koch(t, m)

enter image description here

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Max
  • 243
  • 2
  • 4
  • 9

2 Answers2

8

Quick answer: No

Long answer: The return statement is not required in Python, and reaching the end of the function's code without a return will make the function automatically return None

Additional question: What does a function return?

It returns the objects provided to the return statement if found one. If no return statement is found, None is returned.

Adirio
  • 5,040
  • 1
  • 14
  • 26
5

So I was making a diagram of the recursive function to wrap my head around recursion, and I noticed that apparently every function executes return at the end?

A return itself simply means that the call frame is popped from the call stack, the program counter is set back to state where it was before you made the call, etc. Based on your comment it looks a bit like "backtracking" but mind that - in contrast to backtracking - side effects the functions have, are not reverted/undone. For instance appending to a list, drawing a line, writing to a file, etc. are not reverted. This is the desired behavior in recursion.

Now sometimes it is useful to return a certain value. For instance:

def foo(x,y):
    return x+y

here foo does not only go back to the caller, but first evaluates x+y and the result is returned, such that you can state z = foo(2,3). So here foo is called, 2+3 is evaluated and 5 is returned and assigned to z.

Another question, what exactly does a function return? All of parameters passed to it (assuming multiple parameters)?

In Python all functions return a value. If you specify it like:

return <expression>

the <expression> part is evaluated and that value is returned. If you specify:

return

or no return at all (but you reach the end of the function), None will be returned.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Okay thanks. So in recursion the computer just backtracks then? At the end of each function in column two (say), the computer does not have anything to execute anymore so it goes to where it left off (column one in this case, or "left" generally)? This was the initial motivation of my question really. – Max Mar 10 '17 at 09:35
  • @Max: yes, but *backtracking* is not the correct word. Backtracking means that all that the machine has done would be reverted. That is not the case here (and be extent not in general). But indeed it looks where it has left off and continues. – Willem Van Onsem Mar 10 '17 at 09:36