aspose file tools
The moose likes Beginning Java and the fly likes What is recursion? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "What is recursion?" Watch "What is recursion?" New topic
Author

What is recursion?

Mujahid olabomi Raji
Greenhorn

Joined: Apr 25, 2012
Posts: 1
Can someone give a simple definition,explanation and example(s) on the use of recursion in java?
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 860

definition of recursion: See recursion!


Seriously, this has been asked more than once before.
Please use the search function.


WP
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56233
    
  13

"What is X?" questions are best answered with a google search. Then you can post specific questions that you might have.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
D. Wang
Greenhorn

Joined: Apr 24, 2012
Posts: 3

Recursion is where a function calls itself. It usually starts with a designated "base case" that defines when the method should exit/return. For example:

As you may notice, recursion is almost like a for loop. It actually is sort of like a for loop, except it is usually used when you don't know when the loop should end.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 5902
    
    6

D. Wang wrote:Recursion is where a function calls itself.


It would have been better to let the OP follow the excellent advice he was given to search the web.

As you may notice, recursion is almost like a for loop.


Not really, no.

except it is usually used when you don't know when the loop should end.


No, that's definitely not what separates iteration from recursion. Equivalent knowns and unknowns in the stopping condition can be used in either case.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16695
    
  19

Jeff Verdegan wrote:
As you may notice, recursion is almost like a for loop.


Not really, no.



Recursion can be used to replace a loop. The most common technique of this is a "tail recursion", where the last execution of the code is a recursive call to handle the next iteration.

In most cases though, recursion is used where a loop just isn't feasible -- for example, to walk a tree.... So, as Jeff stated, "Not really. No".

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32712
    
    4
Disagree. A loop is like recursion. If you look at generalised substitution language, the definition of recursion and iteration (loops) are identical, something like
while g do S end =df  (g ⟹ S)^ ; ¬gskip
which is the transitive closure of a guarded substitution, followed by a skip.
I heard that Turing defined structured programming as programming that used sequence selection and recursion, thirty-something years before Böhm and Jacopini defined it as sequence selection and iteration.

And D Wang’s example does not fit his description, I am afraid. Because that is not a “function” calling itself. It is not a “function” because it doesn’t return anything. If it returned something, then it might be recursion. It won’t actually compile because it doesn’t return an int from every path of execution. It would be recursion if it returned a value to be used by itself, however.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4095
    
    1
recursion is easy for the computer, but hard for humans.
many times a recursive program is much shorter and faster than alternative methods, but it is just not the way humans think.
just my opinion.


SCJP
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 4761
    
    7

Campbell Ritchie wrote:I heard that Turing defined structured programming as programming that used sequence selection and recursion, thirty-something years before Böhm and Jacopini defined it as sequence selection and iteration.

Ooof. Too much maths for me. Actually, I liked William's reference; although I believe the quote is:

To understand recursion, you must first understand recursion.



Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32712
    
    4
Winston Gutkowski wrote:. . . Actually, I liked William's reference; although I believe the quote is:

To understand recursion, you must first understand recursion. . . .
Agree. They are so much better, but would any of the greenhorns understand them? I use recursion all the time in my compiler (yes, I know you are not supposed to use recursion in compilers), and I long ago gave up trying to understand it. I simply use it, knowing it will work.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: What is recursion?
 
Similar Threads
This program works but I don't know why...is it recurrsion?
maze program help
Printing a String object without using loop
Tech Word Game
WA #1.....word association