< Algorithms < Find maximum

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

This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.