none
How would you explain recursion and its benefits to a brand new programmer, or non-programmer? RRS feed

  • Question

  • Best explanation to a brand new programmer or nonprogrammer? Perhaps accompanied by a extremely simple code example involving added to 10 or string usages, something REALLY simple.

    Currently trying to understand this concept and doing other reading, but most of it is aimed at very experinced programmers, or people with more knowledge than I. What's your best simple explanation, and we'll vote for the #1.

    Thanks!

    Wednesday, April 22, 2009 4:34 PM

Answers

  • A simple definition of a recursive method in programming is any method that calls itself again and again.

    From my perspective, there are several advantages to recursion:

    1. Some tasks cannot be accomplished without recursion.  These tasks may include exploring hierarchical data structures or something similar.
    2. Recursion can often provide the most succinct and easy to understand versions of a task.  I know this is a C# forum, and C# is an object oriented language, but there are other languages, called functional languages, that heavily leverage recursion.  F# is the newest in the family, and will show up in VS2010.
    3. Some recursive tasks can return values using something called "Lazy Evaluation", which essentially means that values are not calculated until they're needed.  For example, let's say I want to calculate a sequence of numbers that moves on to infinity.  Using a standard list, this would be impossible, but with recursion, you could continue to calculate new values time and and time again until the calling code is finished. 

    Here's a good example of a recursive method that lists all the files in the Windows directory.  This task is most easily accomplished without recursion.

    static void ListFiles(string directory)

    {

        foreach (string file in Directory.GetFiles(directory))

            Console.WriteLine(file);

     

        foreach (string subdirectory in Directory.GetDirectories(directory))

            ListFiles(subdirectory);

    }

     


    David Morton - http://blog.davemorton.net/
    • Marked as answer by Specterunseen Wednesday, April 22, 2009 5:22 PM
    Wednesday, April 22, 2009 4:48 PM
    Moderator

All replies

  • Pretend you're in a large company, and you're the top dog - the CEO.

    You want to make sure everybody in the company knows about the company picnic on Friday.

    You tell all of your VPs about the picnic, and ask them to make sure that all of their people know about it.

    The VPs each tell their own Directors, and ask them to tell their people about it.

    The Directors tell the Managers, and ask them to tell their people about it.

    The Managers tell their employees.  The employees don't have an people under them, so they just say, okay, thanks.


    Recursion is basically just that - instead of having the person at the "top" tell everybody, they delegate as task (tell your people about the picnic) to one level of "people", who then delegate that same task to the people below them, until you have no more people under you.  This makes everybody's lives easier, since one person doesn't have to have a heart attack trying to talk to ten thousand others, and everybody is notified.
    Wednesday, April 22, 2009 4:39 PM
    Moderator
  • A simple definition of a recursive method in programming is any method that calls itself again and again.

    From my perspective, there are several advantages to recursion:

    1. Some tasks cannot be accomplished without recursion.  These tasks may include exploring hierarchical data structures or something similar.
    2. Recursion can often provide the most succinct and easy to understand versions of a task.  I know this is a C# forum, and C# is an object oriented language, but there are other languages, called functional languages, that heavily leverage recursion.  F# is the newest in the family, and will show up in VS2010.
    3. Some recursive tasks can return values using something called "Lazy Evaluation", which essentially means that values are not calculated until they're needed.  For example, let's say I want to calculate a sequence of numbers that moves on to infinity.  Using a standard list, this would be impossible, but with recursion, you could continue to calculate new values time and and time again until the calling code is finished. 

    Here's a good example of a recursive method that lists all the files in the Windows directory.  This task is most easily accomplished without recursion.

    static void ListFiles(string directory)

    {

        foreach (string file in Directory.GetFiles(directory))

            Console.WriteLine(file);

     

        foreach (string subdirectory in Directory.GetDirectories(directory))

            ListFiles(subdirectory);

    }

     


    David Morton - http://blog.davemorton.net/
    • Marked as answer by Specterunseen Wednesday, April 22, 2009 5:22 PM
    Wednesday, April 22, 2009 4:48 PM
    Moderator
  • the above examples are good and I think you already get answer, but here is another way of recursion...

    Subscription...like subscription of magazine, you pay each month.

    Basically, Recursion in general can be seen as something that repeat (History repeats itself, it is recursion of history). In terms of programming language if a functions callback himself it generate recursion.

    Recursion is VERY Resource hungry, and can lead to infinite loops if not used properly. They are used mainly in something like subdirectory (as David's example), finding nth Level of Sub category in products database, in typical E-Commerce website, Family tree generation etc.


    Hope my answer help clarifying concept.

    With Regards,


    Sumit Gupta http://www.vikasumit.com http://www.sumitgupta.net
    Wednesday, April 22, 2009 5:00 PM
  • everyone was very helpful, but so far his is the one that gives a basic explanantion AND a simple code explanation.
    David, do you do this full time, cause you've helped me out quite a bit!  Lurking around in the forum 24/7?
    Wednesday, April 22, 2009 5:22 PM
  • one of the best "simple" explanations, in the style of Headfirst. Great job, and thanks for your help!
    Wednesday, April 22, 2009 5:23 PM
  • David, do you do this full time, cause you've helped me out quite a bit!  Lurking around in the forum 24/7?

    I've learned to type fast and multitask well. :)
    David Morton - http://blog.davemorton.net/
    Wednesday, April 22, 2009 5:29 PM
    Moderator