From: worlman385 on
what are the difference of below 2 factorial function?

the 1st return 0;
the 2nd return factorial result;

what 's the problem of 1st one?

sub factorial
{
$number = $_[0];

if ( $number == 0 ) {
1;
}
else {
$number * factorial( $number - 1 );
}
}

sub factorial
{
if ($_[0] == 0) {
1;
}
else {
$_[0] * factorial($_[0]-1);
}
}
From: Sherm Pendley on
worlman385(a)yahoo.com writes:

> what are the difference of below 2 factorial function?
>
> the 1st return 0;
> the 2nd return factorial result;
>
> what 's the problem of 1st one?

The problem is the same with both - you're relying on an implicit return
of the last expression evaluated. That *happens* to work in the second,
but that's fragile and not recommended; even adding something completely
unrelated to the value you want to return (setting the value of a package
variable, for instance) could affect the value your sub returns.

Another problem is that you're not using 'strict', or properly declaring
your lexical variable $number.

You should use an explicit return() instead:

> sub factorial
> {
> $number = $_[0];

my $number = $_[0];

# Or, more commonly, to allow you to easily add arguments:
# my ($number) = @_;

> if ( $number == 0 ) {
return 1;
> }
> else {
> $number * factorial( $number - 1 );
> }
return $number;
> }
>
> sub factorial
> {
> if ($_[0] == 0) {
return 1;
> }
> else {
return $_[0] * factorial($_[0]-1);
> }
> }

sherm--

--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
From: Mirco Wahab on
worlman385(a)yahoo.com wrote:
> what are the difference of below 2 factorial function?
>
> the 1st return 0;
> the 2nd return factorial result;
>
> what 's the problem of 1st one?
>
> sub factorial
> {
> $number = $_[0];
>
> if ( $number == 0 ) {
> 1;
> }
> else {
> $number * factorial( $number - 1 );
> }
> }

The problem is the 'global' variable $number,
which has no definition within the actual stack
frame, so all accesses to $number refer to the
same variable (which gets overwritten by 0
in the deepest recursion).

Just use

my $number = $_[0];

and thats it.

BTW, next time use the usual prolog
and don't do 'cool' implicicte returns
if more then one return point.

use strict;
use warnings;

sub factorial {
my $number = $_[0];
return 1 if $number == 0;
return $number * factorial( $number - 1 );
}


Regards

M.
From: John Bokma on

Sherm Pendley <spamtrap(a)dot-app.org> wrote:

> worlman385(a)yahoo.com writes:

> # Or, more commonly, to allow you to easily add arguments:
> # my ($number) = @_;

Or if you're sure it's 1 (or for other reasons):

my $number = shift;

>> if ( $number == 0 ) {
> return 1;
>> }


No need for IMO else, because the if returns. I probably would have
written:

$number or return 1;


>> else {
>> $number * factorial( $number - 1 );
>> }
> return $number;

Doesn't work. :-D

else {

return $number * factorial( $number - 1 );
}

or just (without the else):

return $number * factorial( $number - 1 );


--
John

Google Earth and a vacuum cleaner
http://johnbokma.com/mexit/2005/09/15/
From: John Bokma on
Mirco Wahab <wahab-mail(a)gmx.de> wrote:

> sub factorial {
> my $number = $_[0];
> return 1 if $number == 0;
> return $number * factorial( $number - 1 );
> }


or:

sub factorial {

my $number = $_[0];
return $number == 0
? 1
: $number * factorial( $number - 1 )
;
}

--
John

Hypnotize: the track list
http://johnbokma.com/mexit/2005/09/20/