If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Recursion -- Real World Applications?

I was browsing through a book about the C language when I came across recusion. I had used it before at school, simply because I was forced to, even though I could have done the same task with a loop and with a lot less thinking. Anyways, it got me thinking, is there any real world use for recursion that couldnt be done a lot easier with a loop? Do any of you actually use it regularly and if you do, is it by choice? I personally can not find any use for it.

i'm working on a javascript animation library that uses recursion. there are lots of real world applications for recursion, though if you ask me to think of one, i won't be able to. and generally, if you're recursing, then looping won't be easier.

Hmm I've been programming for awhile and never needed it. What in the world is the big O? By the way this is high school course so the teacher didn't want to confuse us all so she just said use loops usually. For instance a factorial function:

Well if you go to college majoring in computer science as I did you will encouter recursion once again but in much greater detail as you will get into analyzing methods in terms of Big O. Typically called O Notation. You will also get into great detail on different methods of sorting such as merge-sort and bubble sort, etc. Often these sorting methods work much faster using recursion. And tell your teacher that she needs to stop misinforming her students. If you write certain sorting algorithms using recursion they will run incredibly faster that by using just plain old loops. I'll dig up some examples if I can find some old programs I wrote. Wrote many of them in college. They are all in java but the concept is still the same. Actually won't look much different than C++.

if you want to list all the values, you will need a lot of loops, and if it's very deep you will never have enough. With recursion you can do it with just a single function, and the array can go deeper and deeper without problem.

You probably never use such big array structure, but think in nested objects (frames & framesets in HTML i.e.), or in animation, when an object divides into two objects and so on

I have used Recursive functions/methods, in several applications across various languages for various reasons. My reasons for doing so were simple, it just made sense. Totaling amounts, quickly sorting data items that are already in memory (Quick Sorts), Getting Data one element at a time, then printing those items out as the function calls return.

While 'yes', you can, in most cases, find a solution via looping constructs, sometimes rescursion can solve complex problems through a simple solution. It's not one of those things that many developers 'go out of their way' to implement, but rather, only use it when needed. The developers that sit around TRYING to find ways to implement recursion, probably also obfuscate their code to no end.

As for college, the principles they attempt to teach you by having you code a small routine(s) using recursion, is use of the stack, and how calls are poped and pushed onto/off of that stack, what FIFO and LIFO are, Etc... It's a very effective means of teaching those concepts.

If you're feeling like this is something you should be using, don't; because it's not something most developers use. I've been doing Medical software development for 12+ years now, and I can still count the number of times I've actually implemented a recusive function call on my two hands.