Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Recursion Confusion

by tobyink (Abbot)
on Apr 27, 2013 at 10:55 UTC ( #1030948=note: print w/ replies, xml ) Need Help??


in reply to Recursion Confusion

I think perhaps your confusion stems from thinking that there is only one variable called $n. In fact, each call to your function creates a brand new $n variable.

Study this. Note that I never add anything to $n - only subtract from it. Now run it to see the output:

use strict; use warnings; sub counter { my ($n) = @_; print "N is $n\n"; if ($n > 0) { counter($n - 1); } print "N is $n\n"; } counter(4);
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name


Comment on Re: Recursion Confusion
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2014-12-21 16:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (106 votes), past polls