JTraining Blog

Share your knowledge.
Bram
Bram

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!!

 


Tagged in: Untagged 
Trackback(0)
Comments (0)Add Comment

Write comment
You must be logged in to post a comment. Please register if you do not have an account yet.

busy

Tags:

Sponsers