return values 
Author Message
 return values

I'm working through Llama book, and I ran into something I don't quite
understand.  The first exercise for chapter 4 asks for a subroutine called
&total, which returns the total of a list of numbers.  When I tried it
myself, I came up with:

sub total {
            my ($sum);

                        $sum += $_;
            }

Quote:
}

$sum = &total(1..5);
print "The numbers add up to $sum";

This never printed anything.  My total sub was apparently returning undef.  
According to the Return Values section of the chapter:

"whatever calculation is last performed in a subroutine is automatically
also the return value."

Why wasnt the last $sum += $_ returned?  Or, why was the last $sum += $_
undef?

Thanks,

Woody



Fri, 24 Sep 2004 08:51:00 GMT  
 return values

Quote:

> I'm working through Llama book, and I ran into something I don't quite
> understand.  The first exercise for chapter 4 asks for a subroutine called
> &total, which returns the total of a list of numbers.  When I tried it
> myself, I came up with:

> sub total {
>             my ($sum);

>                         $sum += $_;
>             }
> }

> $sum = &total(1..5);
> print "The numbers add up to $sum";

> This never printed anything.  My total sub was apparently returning undef.  
> According to the Return Values section of the chapter:

> "whatever calculation is last performed in a subroutine is automatically
> also the return value."

> Why wasnt the last $sum += $_ returned?  Or, why was the last $sum += $_
> undef?

The last $sum += $_(and all others) are trapped inside the foreach {}'s.
It's a scope problem.  The subroutine will return the last calculation at
the *subroutine level*.  Check it out:

use strict;
use warnings;

sub total {
            my ($sum);

                        $sum += $_;
            }
            $sum;       # Doesn't do anything, but it's outside foreach scope.

Quote:
}

my $sum = total(1..5);
print "The numbers add up to $sum\n";


Fri, 24 Sep 2004 15:59:49 GMT  
 return values

Quote:

> The last $sum += $_(and all others) are trapped inside the foreach
> {}'s. It's a scope problem.  The subroutine will return the last
> calculation at the *subroutine level*.  Check it out:

Thanks billy, that makes sense now.
woody


Fri, 24 Sep 2004 20:42:12 GMT  
 return values

Quote:

> I'm working through Llama book, and I ran into something I don't quite
> understand.  The first exercise for chapter 4 asks for a subroutine called
> &total, which returns the total of a list of numbers.  When I tried it
> myself, I came up with:
> sub total {
>             my ($sum);

>                         $sum += $_;
>             }
> }
> $sum = &total(1..5);
> print "The numbers add up to $sum";
> This never printed anything.
> My total sub was apparently returning undef.
> According to the Return Values section of the chapter:
> "whatever calculation is last performed in a subroutine is
> automatically also the return value."
> Why wasnt the last $sum += $_ returned?  Or, why was the last $sum += $_
> undef?

  "...returning undef."
  "...Return Values...."
  "...return value."
  "...returned?"

Clearly your question is answered four times; add a return.

Godzilla!
--

#!perl

print "Content-type: text/plain\n\n";

sub total
 {
  my ($sum);

   { $sum += $_; }
  return $sum;
 }

$sum = &total(1..5);
print "The numbers add up to $sum";

exit;

PRINTED RESULTS:
________________

The numbers add up to 15

--

-----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
   http://www.newsfeeds.com       The Largest Usenet Servers in the World!
------== Over 100,000 Newsgroups - Ulimited downloads - 19 servers ==-----



Fri, 24 Sep 2004 22:04:55 GMT  
 return values


Quote:

>> Why wasnt the last $sum += $_ returned?  Or, why was the last $sum
>> += $_ undef?

>   "...returning undef."
>   "...Return Values...."
>   "...return value."
>   "...returned?"

> Clearly your question is answered four times; add a return.

Yes, I understand now.  However, 'add a return' doesn't directly address
the question I posed (although it probably does aptly answer the
question, "how do i fix this?"):

"Why wasnt the last $sum += $_ returned?  Or, why was the last $sum +=
$_ undef?"

My expected outcome was based on the authors' assertions:
"All Perl subroutines have a return value" and
"Whatever calculation is last performed in a subroutine is automatically
also the return value."

In the sample I created, I consciously relied on the fact that (at least
to my limited grasp) you don't have to explicitly declare a return value
if the automatic return suffices.  With what little Perl I've been
exposed to, I have noticed that people's code tends to be as terse as
possible (often at the expense of comprehensibility), so I attempted to
emulate that style.

Now, I know better.  From now on, I'll explicitly add a return to
subroutines, even if it's nothing more than a t/f to indicate success.



Sat, 25 Sep 2004 00:45:36 GMT  
 return values

Quote:


> >   "...returning undef."
> >   "...Return Values...."
> >   "...return value."
> >   "...returned?"
> > Clearly your question is answered four times; add a return.
> Yes, I understand now.  However, 'add a return' doesn't directly address
> the question I posed (although it probably does aptly answer the
> question, "how do i fix this?"):

There exists no other answer per your parameters.

What answer would you provide with your now
understanding this problem? Do post your
answer for all to read.

Godzilla!

--

-----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
   http://www.newsfeeds.com       The Largest Usenet Servers in the World!
------== Over 100,000 Newsgroups - Ulimited downloads - 19 servers ==-----



Sat, 25 Sep 2004 01:48:26 GMT  
 return values

Quote:



> > >   "...returning undef."
> > >   "...Return Values...."
> > >   "...return value."
> > >   "...returned?"

> > > Clearly your question is answered four times; add a return.

> > Yes, I understand now.  However, 'add a return' doesn't directly address
> > the question I posed (although it probably does aptly answer the
> > question, "how do i fix this?"):

> There exists no other answer per your parameters.

> What answer would you provide with your now
> understanding this problem? Do post your
> answer for all to read.

> Godzilla!

All the guy wants is this
==============================
#!/usr/bin/perl -w
use strict;

sub total {
            my ($sum);

                        $sum += $_;
            }
return $sum;  #just add this line

Quote:
}

my $sum = &total(1..5);  #And the my if your using strict
print "The numbers add up to $sum";

--------------------



Mon, 27 Sep 2004 15:20:59 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. system() return value affected by stderr redirect?

2. Return values from $ftp->put

3. Perl5 bug (in return value from subroutine?)

4. return value $?

5. Sybperl return values

6. Testing subroutine return values

7. Call Perl from ASP & returning values

8. odd return value

9. Return value of a list in scalar context

10. More than one return value

11. The return value of $mode with stat() ...

12. return value from open

 

 
Powered by phpBB® Forum Software