Recursive subroutines: Fibonacci


Recursive definition of Fibonacci


f(1) = 1
f(2) = 1
f(n) = f(n-1) + f(n-2)


examples/subroutines/fibonacci_recursive.pl
#!/usr/bin/perl
use strict;
use warnings;

sub fib {
    my $n = shift;

    if ($n == 1 or $n == 2) {
        return 1 
    }

    return (fib($n-1)+fib($n-2));            # recursive calling
}

print fib(10);                             # calling the function