Algorithms/Find maximum/perl method 1

From Wikibooks, open books for an open world
Jump to: navigation, search

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