Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Multiple recursion & recursion?

Multiple recursion & recursion?

Scheduled Pinned Locked Moved .NET (Core and Framework)
questionhelptutorial
8 Posts 8 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Ali_100
    wrote on last edited by
    #1

    why we need recursion, only for calling method repeatedly ? well there is a factorial problem , we can get the factorial of any number by any loop,so why we need recursion? please don't give formal reason. what is multiple recursion? any example

    M C L S B 6 Replies Last reply
    0
    • A Ali_100

      why we need recursion, only for calling method repeatedly ? well there is a factorial problem , we can get the factorial of any number by any loop,so why we need recursion? please don't give formal reason. what is multiple recursion? any example

      M Offline
      M Offline
      molesworth
      wrote on last edited by
      #2

      waqarnaeem2@hotmail.com wrote:

      why we need recursion

      We need recursion for the things it kills to grow in... sorry, I went all Ah Pook for a minute there :omg: We need recursion because some things are just too messy to do with iteration. Most common recursive algorithms can easily be converted to iterative versions, but will need extra state tracking, like stacks, and then become more complex to write, read and debug. Recursion might have a slight performance penalty in some cases, but the clarity of code usually outweighs that. As the dictionary says Recursion : see Recursion :D

      There are three kinds of people in the world - those who can count and those who can't...

      K 1 Reply Last reply
      0
      • A Ali_100

        why we need recursion, only for calling method repeatedly ? well there is a factorial problem , we can get the factorial of any number by any loop,so why we need recursion? please don't give formal reason. what is multiple recursion? any example

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        waqarnaeem2@hotmail.com wrote:

        please don't give formal reason.

        What?

        Man who stand on hill with mouth open wait long time for roast duck to drop in

        1 Reply Last reply
        0
        • A Ali_100

          why we need recursion, only for calling method repeatedly ? well there is a factorial problem , we can get the factorial of any number by any loop,so why we need recursion? please don't give formal reason. what is multiple recursion? any example

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, we need recursion to model things that are inherently recursive, such as a hierarchical file system, where each folder (including the root) can contain files and folders. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


          1 Reply Last reply
          0
          • A Ali_100

            why we need recursion, only for calling method repeatedly ? well there is a factorial problem , we can get the factorial of any number by any loop,so why we need recursion? please don't give formal reason. what is multiple recursion? any example

            S Offline
            S Offline
            supercat9
            wrote on last edited by
            #5

            Any recursive algorithm can be implemented using non-recursive code if one maintains one's own stack to hold intermediate results. In many cases, however, recursive code can be more efficient and readable. For example, if one has a tree structure which represents an expression (each either holds a value, or else an operation and up to two operands which will yield a value) one may very easily define something like (ignoring the constructor and various other things):

            Class Plus
            Inherits OpNode
            Dim LeftOp, RightOp as OpNode
            Function Eval as Double
            Return LeftOp.Eval + RightOp.Eval
            End Function
            End Class

            While there would be ways to handle such things non-recursively, they would be decidedly awkward. Recursion allows things to be handled very smoothly. If one would be worried about stack overflow, one could do something like:

            Class Plus
            Inherits OpNode
            Dim LeftOp, RightOp as OpNode
            Function Eval(MaxDepth as Integer) as Double
            If MaxDepth < 0 then Throw New WhateverException("Expression too complicated")
            Return LeftOp.Eval(MaxDepth-1) + RightOp.Eval(MaxDepth-1)
            End Function
            End Class

            Still much nicer than maintaining a "to-do list" manually.

            1 Reply Last reply
            0
            • A Ali_100

              why we need recursion, only for calling method repeatedly ? well there is a factorial problem , we can get the factorial of any number by any loop,so why we need recursion? please don't give formal reason. what is multiple recursion? any example

              B Offline
              B Offline
              Baran M
              wrote on last edited by
              #6

              try this [^]

              1 Reply Last reply
              0
              • A Ali_100

                why we need recursion, only for calling method repeatedly ? well there is a factorial problem , we can get the factorial of any number by any loop,so why we need recursion? please don't give formal reason. what is multiple recursion? any example

                P Offline
                P Offline
                Pascal Ganaye
                wrote on last edited by
                #7

                There are a number of problems where a recursive algorithm is both simpler and faster than an loop. For example QuickSort[^] is one of the fastest sorting algorithm that have been found and it is using recursion. http://en.wikipedia.org/wiki/Tower_of_Hanoi[^] is another example where recursion is really paying. There are many more situation where recursion is needed. It makes sense each time you can solve a problem by applying the same methods on its bits. One of the typical example is a function that copy a folder somewhere else. If you were to program it without recursion you would have to write:

                function copyfolder(folder)
                for each file in folder
                copy file
                next
                for each subfolder in folder
                for each file in subfolder
                copy file
                next

                for each subsubfolder in subfolder
                  for each file in subsubfolder
                    copy file
                  next
                  ... what if there is some subsubsubfolders?
                next
                

                next
                end function

                Using a recursive algorithm you would have less code

                function copyfolder(folder)
                for each file in folder
                copy file
                next
                for each subfolder in folder
                copyfolder(subfolder) ... this line is where the recursion happen
                next
                end function

                Signature Seen in CodeProject forums : Is it a sort of male dominance thing? You know, who can be the most witty? Who considers themselves the clown at the party? Is it a follow the leader thing? One can't be seen not to have an elaborate signature, it's just no right, after all, all the other people have one. (Is that long enough?)

                1 Reply Last reply
                0
                • M molesworth

                  waqarnaeem2@hotmail.com wrote:

                  why we need recursion

                  We need recursion for the things it kills to grow in... sorry, I went all Ah Pook for a minute there :omg: We need recursion because some things are just too messy to do with iteration. Most common recursive algorithms can easily be converted to iterative versions, but will need extra state tracking, like stacks, and then become more complex to write, read and debug. Recursion might have a slight performance penalty in some cases, but the clarity of code usually outweighs that. As the dictionary says Recursion : see Recursion :D

                  There are three kinds of people in the world - those who can count and those who can't...

                  K Offline
                  K Offline
                  kanagarajm
                  wrote on last edited by
                  #8

                  I have wrote a program to get the file system with folders and files up to leaf nodes without using recursion. If you are interested you can send a mail to kanagarajmk@gmail.com

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups