First program

7 views
Skip to first unread message

darshan kr

unread,
May 17, 2012, 12:57:09 PM5/17/12
to sjbit...@googlegroups.com
Hai Everyone,

# a subroutine that sums the elements in an array
# and returns the sum

sub sum_array {
    my(@vals) = @_;        # put parameters in array @vals
    my($sum) = 0;       # initialize the sum to 0

    # $sum is private so we don't need to worry about
    # clobbering any global variable named $sum!

    foreach $i (@vals) {
	$sum = $sum + $i;
    }
    return($sum);
}

# main program
print "Enter a bunch of numbers on a line\n";
$_ = ;     
@nums = split;
print "The sum is ", &sum_array(@nums), "\n";


Reply all
Reply to author
Forward
0 new messages