11-07-2002, 12:30 AM
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.
11-07-2002, 02:17 AM
11-07-2002, 03:09 AM
Well if you studied recursion then must also be familiar with analyzing methods to determine things like the big O and all that.
It all depends on what you need to do. Recursion can be less resource intensive in certain situations where the equivalent non-recursive method eats up too many resources. Works both ways.
11-07-2002, 04:19 AM
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:
int fact(int n)
if(n<=1) return 1;
else return n * fact(n-1);
could just as easily be written using a loop:
int fact(int n)
int product = 1, i;
product *= i;
and recursion I heard is often much more memory intensive and memory is vital when programming the gameboy.
11-07-2002, 04:51 AM
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++.
11-07-2002, 11:53 AM
Let's see if a deep array is a good example:
data='and more and more'
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
11-08-2002, 03:58 PM
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.
... course, I use recursion to count my fingers.