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. C / C++ / MFC
  4. Recursive function - how to correctly implement return?

Recursive function - how to correctly implement return?

Scheduled Pinned Locked Moved C / C++ / MFC
hardwaredata-structureshelptutorialquestion
8 Posts 6 Posters 0 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.
  • V Offline
    V Offline
    Vaclav_
    wrote on last edited by
    #1

    I have a function which recursively scans an array for a matching value and when found processes it and returns. Since it works recursively a single return obviously won't do. I was hoping I could have a static counter of the recursive calls but have no idea how to implement multiple returns. I am doing this on Arduino, so KISS is important to me. Any help would be appreciated. Thanks Vaclav

    Richard Andrew x64R _ S CPalliniC 4 Replies Last reply
    0
    • V Vaclav_

      I have a function which recursively scans an array for a matching value and when found processes it and returns. Since it works recursively a single return obviously won't do. I was hoping I could have a static counter of the recursive calls but have no idea how to implement multiple returns. I am doing this on Arduino, so KISS is important to me. Any help would be appreciated. Thanks Vaclav

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      Just make the function return true if a matching value is found and false otherwise. Then, when a return of true is received, simply exit the function without further processing and this will unwind the stack.

      The difficult we do right away... ...the impossible takes slightly longer.

      1 Reply Last reply
      0
      • V Vaclav_

        I have a function which recursively scans an array for a matching value and when found processes it and returns. Since it works recursively a single return obviously won't do. I was hoping I could have a static counter of the recursive calls but have no idea how to implement multiple returns. I am doing this on Arduino, so KISS is important to me. Any help would be appreciated. Thanks Vaclav

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        It is not really obvious if recursion needs multiple return statements. It all really depends on the logic being implemented. So if you've implemented it and are not getting the expected results, posting the relevant code should get the good people here to help you figure out the problem.

        «_Superman_»  _I love work. It gives me something to do between weekends.

        _Microsoft MVP (Visual C++) (October 2009 - September 2013)

        Polymorphism in C

        V 1 Reply Last reply
        0
        • V Vaclav_

          I have a function which recursively scans an array for a matching value and when found processes it and returns. Since it works recursively a single return obviously won't do. I was hoping I could have a static counter of the recursive calls but have no idea how to implement multiple returns. I am doing this on Arduino, so KISS is important to me. Any help would be appreciated. Thanks Vaclav

          S Offline
          S Offline
          Stefan_Lang
          wrote on last edited by
          #4

          1. It is always possible to make do with a single return. I'm not sure what makes you think otherwise. 2. searching an array for a matching value is a trivial operation - why did you implement it recursively? What is so difficult in your search algorithm that it prevents you from implementing it in a simple loop? 3. having a static counter for depth of recursion is a common method, and it helps catching endless recursions. But, generally speaking, static variables can cause many issues - if there is even a remote chance you're going to use multithreading sometime in the future, just forget about it! Either way, a safer method is to create a context and pass a reference (i. e. pointer in C/C++) to the recursive function. That context object may contain a counter or any other mechanism to control recursion depth, but it won't be static: it lives in the context of the thread that initialiy called the recursive function, and it won't cause conflicts when that function is called from different clients at the same time.

          GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

          V 1 Reply Last reply
          0
          • S Stefan_Lang

            1. It is always possible to make do with a single return. I'm not sure what makes you think otherwise. 2. searching an array for a matching value is a trivial operation - why did you implement it recursively? What is so difficult in your search algorithm that it prevents you from implementing it in a simple loop? 3. having a static counter for depth of recursion is a common method, and it helps catching endless recursions. But, generally speaking, static variables can cause many issues - if there is even a remote chance you're going to use multithreading sometime in the future, just forget about it! Either way, a safer method is to create a context and pass a reference (i. e. pointer in C/C++) to the recursive function. That context object may contain a counter or any other mechanism to control recursion depth, but it won't be static: it lives in the context of the thread that initialiy called the recursive function, and it won't cause conflicts when that function is called from different clients at the same time.

            GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

            V Offline
            V Offline
            Vaclav_
            wrote on last edited by
            #5

            Thanks Stefan, I will take another look at the process to see if I can do it using loop(s).

            D 1 Reply Last reply
            0
            • _ _Superman_

              It is not really obvious if recursion needs multiple return statements. It all really depends on the logic being implemented. So if you've implemented it and are not getting the expected results, posting the relevant code should get the good people here to help you figure out the problem.

              «_Superman_»  _I love work. It gives me something to do between weekends.

              _Microsoft MVP (Visual C++) (October 2009 - September 2013)

              Polymorphism in C

              V Offline
              V Offline
              Vaclav_
              wrote on last edited by
              #6

              You may be right that I am not sure about the "returns". But debugging in IDE which lacks debugger you kinda flying blindfolded. I'll try different way to skin this cat!

              1 Reply Last reply
              0
              • V Vaclav_

                Thanks Stefan, I will take another look at the process to see if I can do it using loop(s).

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                And if your array is sorted, your search can go from O(N) to O(log N).

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                1 Reply Last reply
                0
                • V Vaclav_

                  I have a function which recursively scans an array for a matching value and when found processes it and returns. Since it works recursively a single return obviously won't do. I was hoping I could have a static counter of the recursive calls but have no idea how to implement multiple returns. I am doing this on Arduino, so KISS is important to me. Any help would be appreciated. Thanks Vaclav

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #8

                  Have a look at this: "Recursive Functions"[^].

                  THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

                  In testa che avete, signor di Ceprano?

                  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