programming-examples/perl/Subroutine/Recursive factorial subroutine.pl
2019-11-15 12:59:38 +01:00

16 lines
301 B
Perl

foreach ( 0 .. 10 ) {
print "$_! = " . factorial( $_ ) . "\n";
}
sub factorial
{
my $number = shift; # get the argument
if ( $number <= 1 ) { # base case
return 1;
}
else { # recursive step
return $number * factorial( $number - 1 );
}
}