Please send questions to
st10@humboldt.edu .
#!/usr/bin/perl -w
#######################################################
# lect03_my3
#
# a subroutine to add up its arguments
# (return their sum) - NOW using lexical variable!
# and now using setting parameters to "nicer" name.
#
# modified by Sharon Tuttle from "Learning Perl",
# by Schartz and Phoenix
#
# last modified: 9-6-04
#######################################################
#----------------------------------------------------
# sum all arguments and return the sum ---
# now using better style still (lexical variables,
# some of which "name" the parameters with more
# mnemonic/meaningful names!
#----------------------------------------------------
sub sum_vals4
{
# give arguments a nicer name:
my(@list_of_nums) = @_;
# $sum is just a local variable, not a parameter
# (no @_ here, get it?)
my($sum) = 0;
foreach $val ( @list_of_nums )
{
$sum += $val;
}
return $sum;
}
print &sum_vals4(1, 2, 3, 4, 5), "\n";
print "enter numbers to add up, each on their own line\n";
print " (control-d to stop):\n";
chomp(@vals = <STDIN>);
print &sum_vals4(@vals), "\n"
# end of lect03_my3