Algorithms/Find maximum/perl method 1

perl source code

sub findmax() {
 
        return 0 if $#_ == -1;
 
        return $_[0] if $#_ == 0;
 
        my $curr_max = $_[0];
        foreach (@_) {
                $curr_max = $_ if $_ > $curr_max;
        }
        return $curr_max;
}

Simple example of using this subroutine:

print &findmax(qw /1 5 3 199 2/);

output should be: 199

Last modified on 25 April 2009, at 03:52