subroutine's returned value

X

xyoavx

Hello,
I don't understand why the following program displays nothing, although
the meaning of the last calculation performed by the subroutine max
is:{$max_so_far=10;}

#!perl -w

$maximum=&max(3,5,10);
print $maximum,"\n";
sub max
{
my($max_so_far)=shift @_;
foreach (@_)
{
if ($_>$max_so_far)
{$max_so_far=$_;}
}
}

Thanks in advance,
xyoavx
 
P

Paul Lalli

xyoavx said:
Hello,
I don't understand why the following program displays nothing, although
the meaning of the last calculation performed by the subroutine max
is:{$max_so_far=10;}

#!perl -w

$maximum=&max(3,5,10);
print $maximum,"\n";
sub max
{
my($max_so_far)=shift @_;
foreach (@_)
{
if ($_>$max_so_far)
{$max_so_far=$_;}
}
}

This issue was brought up in this group about 5 or 6 weeks ago, I
believe [1]. The $max_so_far=$_ is NOT the last statement executed by
Perl. The last statement executed is the check of @_ to determine if
there are any more iterations of the foreach loop. There are not, so
the loop ends, and then the subroutine ends.

Paul Lalli

[1]http://groups.google.com/group/comp...3a2d491a531?q=return+value+of+for+loop&rnum=1
 
X

xyoavx

Hello Paul Lalli,
Thanks a lot.
Regards,
xyoavx
Paul said:
xyoavx said:
Hello,
I don't understand why the following program displays nothing, although
the meaning of the last calculation performed by the subroutine max
is:{$max_so_far=10;}

#!perl -w

$maximum=&max(3,5,10);
print $maximum,"\n";
sub max
{
my($max_so_far)=shift @_;
foreach (@_)
{
if ($_>$max_so_far)
{$max_so_far=$_;}
}
}

This issue was brought up in this group about 5 or 6 weeks ago, I
believe [1]. The $max_so_far=$_ is NOT the last statement executed by
Perl. The last statement executed is the check of @_ to determine if
there are any more iterations of the foreach loop. There are not, so
the loop ends, and then the subroutine ends.

Paul Lalli

[1]http://groups.google.com/group/comp...3a2d491a531?q=return+value+of+for+loop&rnum=1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top