- recursive
Recursive subroutines: factorial
Mathematical definition of factorial:
n! = n * (n-1) * (n-2) * ... 1 1! = 1 n! = n * (n-1)!
examples/subroutines/factorial.pl
#!/usr/bin/perl use strict; use warnings; my $n = shift or die "Usage: $0 NUMBER\n"; my $result = factorial($n); print $result; sub factorial { my ($n) = @_; if ($n == 1) { return 1; } my $prev = factorial($n - 1); return $n * $prev; }