Woot I think I got it. Check it out (I also added comments)
use warnings; use strict; my $i = $#ARGV; #Scalar for array length if ($ARGV[0] eq "add") { #Checks for add my $rtn = add(@ARGV[1..$i]); #Calls add subroutine print "The sum is: $rtn"; } elsif ($ARGV[0] eq "multiply"){ #Checks for multipl +y my $rtn = multiply(@ARGV[1..$i]); #Calls multiply subroutine print "Their product is: $rtn"; } sub add { #add subroutine my ($sum); $sum=0; foreach $_(@_) { #Loops through the array $sum +=$_; #Adds each array element to the r +est } return $sum; #Returns the sum value } sub multiply { #multiply subroutine my $product=1; foreach $_(@_) { #Loops through the array $product*=$_; #Multiplys each element to last } return $product; #Returns the product value }
How does that look?
In reply to Re^2: Simple add and multiply subroutines
by negzero7
in thread Simple add and multiply subroutines
by negzero7
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |