Z
Zachary Turner
Hello,
I'm just learning Perl and I'm going through a book and there was an
exercise in one of the chapters to write a simple subroutine to add up
all the values that were passed as arguments. Simple enough, I
implemented this as follows:
sub total {
my $sum;
foreach (@_) {
$sum += $_;
}
return $sum;
}
However, in the same chapter it says that if you do not put a return
statement, the return value of the function is the result of the last
calculation that occured in the function. So to test this I deleted
the "return $sum;" line from the function. When the return line was
there, it returned the correct value. Without that line, it appears
to return undef.
Can anyone explain?
Thanks
I'm just learning Perl and I'm going through a book and there was an
exercise in one of the chapters to write a simple subroutine to add up
all the values that were passed as arguments. Simple enough, I
implemented this as follows:
sub total {
my $sum;
foreach (@_) {
$sum += $_;
}
return $sum;
}
However, in the same chapter it says that if you do not put a return
statement, the return value of the function is the result of the last
calculation that occured in the function. So to test this I deleted
the "return $sum;" line from the function. When the return line was
there, it returned the correct value. Without that line, it appears
to return undef.
Can anyone explain?
Thanks