sub get_max_index { #revisit how to define functions/"my" var ? (default parameters?)
my $imax=0;
for (my $i=0; $i<@_; $i++)
{
$imax=$_[$i] if ($imax<=$_[$i]);}
return $imax;
}
####
use strict;
use warnings;
sub get_max_index {
my (@copyOfArray) = @_; #make an explicit local copy
my $imax= shift @copyOfArray; # modifies this copy, but ok
for (@copyOfArray){ # no indices...
$imax = $_ if $imax < $_;
}
return $imax;
}
my @arr = (1..10);
my $ans = get_max_index(@arr);
print"$ans\n";
##
##
use strict;
use warnings;
sub get_max_index
{
my $arrayRef = shift;
my $imax = $arrayRef->[0]; #don't modify @arr
for (@$arrayRef){
$imax = $_ if $imax < $_;
}
return $imax;
}
my @arr = (1..10);
my $ans = get_max_index(\@arr); #pass reference to array!
print"$ans\n";