#!/usr/bin/perl -w use strict; # # function.pl # This is illustrates how Perl functions work. Note, even though it # does not use the word function like most other languages, it does # behave like a function in that it can be passed arguments and return # a value, an array, a filename etc. # # Input value is an array to be summed, i.e. # declare Function sub total; # declare variables # returned sum my $sum = 0; #for summing up original array items my $item = 0; # Used to get the result of calling the sum function my $result = 0; # set input arrays to null array my @data1 = (); my @data2 = (); # Start of Function/Sub Routine Section sub total # Returns value $sum { $sum = 0; foreach $item (@_) { $sum += $item; # add $item to $sum, i.e. $sum = $sum + item } return $sum; } # Main Program Section @data1 = (1,2,3,4); $result = total(@data1); print "Sum of @data1 is $result\n"; @data2 = (1,2,3,4,5); $result = total(@data2); print "Sum of @data2 is $result\n"; $result = total(@data1, @data2); print "Sum of @data1 and @data2 is $result\n"; # End Main Program Section