Blob


1 #!/usr/bin/perl
3 #Write a subroutine, named total, which returns the total of a list of numbers. Hint: the subroutine should *not* perform any I/O; it should simply process its parameters and return a value to its caller. Try it out in this sample program, which merely exercises the subroutine to see that it works. The first group of numbers should add up to 25.
5 use warnings;
6 use strict;
7 use utf8;
9 sub total {
10 my $sum = 0;
11 foreach (@_) {
12 $sum += $_;
13 }
14 return $sum;
15 }
17 my @fred = qw{ 1 3 5 7 9 };
18 my $fred_total = total(@fred);
19 print "The total of \@fred is $fred_total.\n";
20 print "Enter some numbers on separate lines: ";
21 my $user_total = total(<STDIN>);
22 print "The total of those numbers is $user_total.\n";