I can't understand how recursive functions where the function is called multiple times within the same function works (multiple recursion I believe). I was wondering if someone could explain it me.
I'm a beginner and learning how to code in python. I got to a stage where, using turtle, I was shown a recursive function and had to guess what it would draw. I got it completely wrong but it drew a tree-like diagram.
import turtle
t = turtle.Turtle()
def draw (t, length, n):
if n == 0:
return
angle = 50
t.fd(length*n)
t.lt(angle)
draw(t, length, n-1)
t.rt (2*angle)
draw(t, length, n-1)
t.lt(angle)
t.bk(length*n)
I understand completely how it draws the first branch but once n=1 I get confused as I assumed that at that point when draw(t, length, n-1) is called that n=0 so the function is returned and nothing more happens. However, it does something completely different and I was wondering what the order of operations was and why it does that. I know what it does but I just don't know why.
returnonly returns to the most recent caller, not all the way back to the very first call.