On Sun Jan 18 2009 @ 10:59, dolphin_sonar wrote:
>    1 # When calling 'running_sum(5, 6);' the variable 'state @numbers'
> receives those two
>    2 # parameters, (5 and 6), right? Then, the @numbers array also
> copies/stores (5 and 6)
>    3 # into the special '( @_ )' variable as well, right? Also, line
> 13 pushes '$number' into
>    4 # the '@numbers' array each time through the foreach loop,
> right?

Maybe I'm not understaning what you mean, but I think you're confused. The
arguments to a subroutine go into the @_ array. The @numbers array is
empty until you load it up in the foreach loop. Having 5 and 6 as arguments
does not automatically put those items into @numbers, nor does @numbers
copy anything into @_.

Also, I understand that you may just be testing out persistent variables in
5.10, but this program is confusing.

Consider what happens if you call running sum a second time (say as
running_sum( 2, 7 );) If I add that call, here's my output:

    telemachus ~ $ perl sum
    The sum of (5 6) is 11
    The sum of (5 6 2 7) is 31

That appears to say that perl has added 5, 6, 2, and 7 up to 31. What
actually happened was that you added 5, 6, 2 and 7 (20) to 11 (the sum from
the previous call to running_sum - which was saved). To put this another way
around: maybe you want to keep a running sum, but the print statement in the
subroutine is very confusing.

Hope this helps, T

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to