Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2016-10-24 16:48 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (309 votes). Check out past polls.