# RECURSION

• April 20th, 2013, 03:24 AM
kevthanewversi
RECURSION
Could someone explain recursion to me please. I get that a function loops coz it is called several times but I can't get why and how! Frustration.
• April 20th, 2013, 08:30 AM
mariostg
Re: RECURSION
You are probably missing an exit condition. Show the codes you have.
In a nutshell:
Code :

```if condition is met return else call function again```
• April 20th, 2013, 02:22 PM
jps
Re: RECURSION
Thread moved from "Whats wrong with my code?"
• April 20th, 2013, 02:38 PM
myjava
Re: RECURSION
Recursion simply means recurring again and again. In another word, it is function calling the function itself until certain condition is met. For example:- If you look at the Fibonacci number(fibonacci number is sequence of number where the next number is the addition of past two numbers). The fibonacci numbers are 0,0,1,1, 2, 3, 5, 8. 8 is obtained by adding 5 and 3, 5 is obtained by adding 2 and 3 and so on. This can be generated easily by recursion.
Anything that can be done with recursion can also be done iteratively or without using recursion. When you use recursion it keeps your code short. However, in java it is advisable not to use recursion because of the performance and memory issue. But it doesn't mean recursion is bad. In other programming language like LISP, recursion is the best way of solving problems. Don't bother too much about it. Just learn its basics and how it works. You will hardly use recursion in future if you continue in java.
• April 21st, 2013, 12:04 AM
kevthanewversi
Re: RECURSION
Quote:

Originally Posted by myjava
Recursion simply means recurring again and again. In another word, it is function calling the function itself until certain condition is met. For example:- If you look at the Fibonacci number(fibonacci number is sequence of number where the next number is the addition of past two numbers). The fibonacci numbers are 0,0,1,1, 2, 3, 5, 8. 8 is obtained by adding 5 and 3, 5 is obtained by adding 2 and 3 and so on. This can be generated easily by recursion.
Anything that can be done with recursion can also be done iteratively or without using recursion. When you use recursion it keeps your code short. However, in java it is advisable not to use recursion because of the performance and memory issue. But it doesn't mean recursion is bad. In other programming language like LISP, recursion is the best way of solving problems. Don't bother too much about it. Just learn its basics and how it works. You will hardly use recursion in future if you continue in java.

Thanks a lot man and how does execution go about when the method gets called again? The flow of execution.
• April 21st, 2013, 06:56 AM
Norm
Re: RECURSION
Each call to the method is the same as any other call to any other method.