Recursive factorial subroutine : Recursive « Subroutine « Perl






Recursive factorial subroutine

   

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 );
   }
}

   
    
    
  








Related examples in the same category

1.Recursive fibonacci function.
2.Recursive subroutine
3.Write recursive subroutines
4.calculate 1000th element of standard Fibonacci sequence
5.factorial with recursive function
6.A recursive subroutine to perform arithmetic.