There's more than one way to do things | |
PerlMonks |
Re: recursion basicsby fullermd (Priest) |
on Jun 23, 2015 at 09:22 UTC ( [id://1131590]=note: print w/replies, xml ) | Need Help?? |
No, it has to finish the binary($k) before it can do the print "below\n". And that binary() call has its own above/binary()/below sequence to follow. It would be like saying:
You wouldn't expect that to output "x/z/y", would you? Each line has to finish before the next one starts, and the recursive binary() call will (from the top) also involve another recursive call before it returns to the original call. It may be simpler to look a version without the calculations, just showing the call path:
So now we're just calling ourselves $steps times, and noting where we go. Output:
So what happens? We start off by calling recur() with 0. So that says it's doing so, then re-calls itself with 1. That says it's doing so, re-calls with 2. Says it's doing so, re-calls with 3. Now the 3 notices that's our limit, and so it returns. It returns back to the re-call that had 2, which now completes, and returns. It returns back to the re-call that had 1, which now completes, and returns. It returns back to the original call with 0, which returns. And then it falls off the end of the script.
In Section
Seekers of Perl Wisdom
|
|