Recursive function - how to correctly implement return?
-
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
-
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
Just make the function return
true
if a matching value is found andfalse
otherwise. Then, when a return oftrue
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.
-
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
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)
-
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
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)
-
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)
-
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)
-
Thanks Stefan, I will take another look at the process to see if I can do it using loop(s).
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
-
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
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