子例程 subroutine

 

#! /usr/bin/perl
use strict;
use warnings;
print "\n---------summation_STDIN_parameter----------\n";
sub total{
    my $array_len = @_;
    my $sum = 0;
    foreach(0..$array_len){
        $sum += $_[$_];
        #sum += 1;
    }
    return $sum;
}
my @comma = qw{ 3 5 1 7 9};
my $dash = &total(@comma);
print "Sum is: $dash.\n";
print "Enter some numbers on seperate lines:";
my $dollar_sign = &total(<STDIN>);
print "Sum of those numbers is: $dollar_sign.";
print "\n---------summation_STDIN_parameter----------\n";
        
print "\n---------_sum_1..1000----------\n";
#my $dot_operator;
my $dot_operator = &total(1..1000);
print "Sum of 1..1000 is: $dot_operator.";
print "\n---------_sum_1..1000----------\n";

posted @ 2019-06-11 15:51  jimy1  阅读(250)  评论(0编辑  收藏  举报
Total Visits
AmazingCounters.com