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?"

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!

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?"

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.

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?"
> 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?"):

understanding this problem? Do post your

Godzilla!

--

-----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
http://www.newsfeeds.com       The Largest Usenet Servers in the World!

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

Quote:

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

> > 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?"):

> understanding this problem? Do post your

> 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

 Page 1 of 1 [ 7 post ]

Relevant Pages