Exercises

4.12 Exercises

See Section A.3 for answers to the following exercises:

1.       [12] Write a subroutine, called &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.

2.          my @fred = qw{ 1 3 5 7 9 };
3.          my $fred_total = &total(@fred);
4.          print "The total of \@fred is $fred_total.\n";
5.          print "Enter some numbers on separate lines: ";
6.          my $user_total = &total(<STDIN>);
print "The total of those numbers is $user_total.\n";

7.       [5] Using the subroutine from the previous problem, make a program to calculate the sum of the numbers from 1 to 1000.

 



Learning Perl
Learning Perl, 5th Edition
ISBN: 0596520107
EAN: 2147483647
Year: 2001
Pages: 205

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net