Please send questions to st10@humboldt.edu .
#!/usr/bin/perl -w

#######################################################
# lect03_return3
#
# a subroutine to add up its arguments
# (return their sum) - NOW using lexical variable!
# and now using setting of a parameters to nice name
# and return, too;
# ...and it returns undef if list is empty
# 
# modified by Sharon Tuttle from "Learning Perl",
#    by Schartz and Phoenix
#
# last modified: 9-6-04
#######################################################

#---------------------------------------------------
# sum the arguments and return their sum ---
#    but return undef if NO arguments are given.
#---------------------------------------------------

sub sum_vals5
{
    # give arguments a nicer name:

    my(@list_of_nums) = @_; 

    # (note use of list in a scalar context to get its 
    #    size...)

    if ( @list_of_nums == 0 )       
    {
        return;
    }

    # $sum is a local variable, not a parameter

    my($sum) = 0;

    foreach $val ( @list_of_nums )
    {
        $sum += $val;
    }
    return $sum;
}

print &sum_vals5(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>);

$result = &sum_vals5(@vals);

if (defined($result))
{
    print "returned $result\n\n";
}
else
{
    print "returned undef\n\n";
}



# end of lect03_return3