Hi,
Here is a simple program:
1 my @nums = (1..100);
2 my $result = &total(@nums);
3 4 print "$result\n";
5 6 sub total {
7 my $sum;
8 foreach(@_){ $sum += $_ }
9 $sum;
10 }
My question is without line 9, the return value is nothing, while I expect it will return the value of $sum, as it is the last expression being evaluated, is it correct?
Actually, C<foreach> is the last statement in your routine, and it has no meaningful value. Note also that the variation:
sub sum { my $sum = 0; $sum += $_ for @_; }
does not work either though I think it probably should.
Regardless, I think it is good practice to always return explicitly with a C<return> statement. It makes the code clearer and easier to read.
Randy.
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>