Recursive Methods E-mail
Bram
Wednesday, 13 January 2010 00:26

Here is just a small intro to recursive methods; Basically, a recursive method is a method that calls itself; e.g.

int fibonacci(int n) {
    if(n <= 1)     {
        return n;
    }     else     {
        return fibonacci(n - 1) + fibonacci(n - 2);
    }
}    (this method returns the n'th value of the Fibonacci sequence)

As you can see in the example above, the part that makes this method recursive is:
       return fibonacci(n - 1) + fibonacci(n – 2);
because there, the method calls his own function again.

 In my opinion there are benefits to recursive methods ( but not always ) like: a problem can be defined as a recursive problem or because of a recursive method your programming code can be shorter.


Anyway, be carefull not to end up in an endless loop!!