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?
5 # For instance, it pushes 5 to the end of the array the first
time around,
6 # then pushes 6 to the end of the array the next time around.
7
8 sub running_sum {
9 state $sum = 0;
10 state @numbers;
11
12 foreach my $number ( @_ ) {
13 push (@numbers, $number);
14 $sum += $number;
15 }
16 say "The sum of (@numbers) is $sum";
17 }
18
19 running_sum( 5, 6 );
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/