Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jun 2002
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #3
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #4
    New Coder
    Join Date
    Jun 2002
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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;
    for(i=n;i>0;i--)
    product *= i;
    return product;
    }

    and recursion I heard is often much more memory intensive and memory is vital when programming the gameboy.

  • #5
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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++.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #6
    Regular Coder
    Join Date
    Aug 2002
    Location
    Spain
    Posts
    420
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Let's see if a deep array is a good example:


    data[0]=
    data[0][0]='one'
    data[0][1]='two'
    data[0][2]='three'
    data[1]=
    data[1][0]='1'
    data[1][1]='2'
    data[1][2]=
    data[1][2][0]='sub3'
    data[1][2][1]='sub4'
    data[1][2][2]=
    data[1][2][2][0]='more'
    data[1][2][2][1]=
    data[1][2][2][1][0]='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
    Don't resist to assimilation. Billions of Borgs can't be wrong!

  • #7
    New Coder
    Join Date
    Oct 2002
    Location
    Atlanta
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by dhtroy; 11-08-2002 at 03:00 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •