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);
In reply to Re: Recursion Confusion
by tobyink
in thread Recursion Confusion
by live4tech
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |