Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: recursion basics

by wrinkles (Pilgrim)
on Jun 23, 2015 at 06:35 UTC ( [id://1131570]=note: print w/replies, xml ) Need Help??


in reply to Re: recursion basics
in thread recursion basics

Even slow that's a challenge. Why does the final return only run once? I mean, why doesn't the return interrupt the unwinding earlier?

Replies are listed 'Best First'.
Re^3: recursion basics
by aaron_baugher (Curate) on Jun 23, 2015 at 06:48 UTC

    Check my first reply for a more detailed explanation, but the short version is that, each time the function calls itself, the caller stops and waits for the "child" function to finish. So none of the instances reach that final return until one of them returns from the first return, then they all finish and hit the second return in reverse order. Something like this:

    binary1 calls binary2, stops and waits for it binary2 calls binary3, stops and waits for it binary3 calls binary4, stops and waits for it binary4 returns from the first return line ($n==1) binary3 finishes, returns binary2 finishes, returns binary1 finishes, returns print statement executes, printing binary1's return value

    Aaron B.
    Available for small or large Perl jobs and *nix system administration; see my home node.

Re^3: recursion basics
by robby_dobby (Hermit) on Jun 23, 2015 at 06:43 UTC
    Read my post here. Hopefully, it should make it clear to you.
Re^3: recursion basics
by Anonymous Monk on Jun 23, 2015 at 09:32 UTC

    Even slow that's a challenge. Why does the final return only run once? I mean, why doesn't the return interrupt the unwinding earlier?

    Um, try this, and ask yourself what happens

    Rip(); sub Rip { print "Rip on\n"; Van(); Winkle(); print "Rip off\n"; } sub Van { print "Van on\n"; Winkle(); print "Van off\n"; } sub Winkle { print "Winkle\n"; } __END__ Rip on Van on Winkle Van off Winkle Rip off

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1131570]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-20 03:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found