#! /usr/bin/perl # sum # add up elements of array sub sum { for ($i = 0; $i < @_; $i++) { # $s is the sum of the first $i array elements # $s == $_[$0] + .. + $_[$i-1] $s = $s + $_[$i]; } return $s; } # main program @a1 = (2, 4, 6, 8); print "The sum of elements in @a1 is ", sum(@a1), "\n";