use strict; use warnings; my @numbers = qw(4 12 18 21 35); my $average = find_average(\@numbers); print "The average found for this list is: $average \n" ; ## find_average will cause a fatal error if ## there are no numbers in the array. sub find_average { # subroutine gets a sinle value which is a reference # to an array, not an arry itself. This is much, much # faster if say the array has say 1,000 elements. my $num_array_ref = shift; # could be: my ($num_array_ref) = @_; # The shift operation is very slightly faster if # only one value is involved. my $sum; # no need to set $sum=0; # but I wouldn't quibble if you did that. foreach my $num (@$num_array_ref) { $sum += $num; } my $avg = $sum / @$num_array_ref; return $avg; } __END__ The average found for this list is: 18