Wondering if I have this straight .. There is a danger with a Recursive procedure to cause a Stack Overflow. The solution is to use Static variables. I see where the recursive Quick Sort does not use Static variables (at least I have never declared Static variables when I use the Quick Sort method). So I'm thinking, if I create a Recursive procedure whereby a math formula is deployed (+,-,*,/) then this is were the danger lays. If the Recursion is simple comparisons, or True/False, or re-ordering then there is no threat to Overflow the Stack. Is that correct?
I think it's worth explaining exactly what a stack overflow is and why it occurs:
Every time the CPU jumps to a subroutine (a SUB or a FUNCTION), it needs to remember where in the code it came from, so it can go back there when it's finished.
The way it does this is by pushing its current address to the stack before jumping. But really, you can think of this as just having a big array in memory, and every time you need to do a subroutine you REDIM _PRESERVE the array to increase the size by one, add your current code position to the new element, then go to the subroutine. After you finish and return to where you came from, you can REDIM the array again to make it one element smaller (since you don't need the value any more).
Now, because of Reasons™ the program can only make this array so big, about 8MB in the best of cases (it could be quite a bit less). This means you can only have so many subroutines-that-call-subroutine, because eventually you'll run out of space to store these return locations. Note that this isn't a limit on the total number of subroutine calls we can make in the program in total, because whenever a subroutine returns it frees up an element of our array. It's just a limit on how many calls you can have chained together.
For further Reasons™ the data structure used to store these return locations is in fact a stack, and if it gets full it goes beyond its bounds, hence a "stack overflow" (in the real world the stack also grows
downwards so the visual metaphor is a little messed up, but ignore that).
You can see now why recursion can be problematic - it's a very quick way to make many subroutine calls and fill up the stack with return locations. It doesn't matter what you're doing in the subroutine, if you're recursing you just have to make sure you have a limit on how deep the recursion goes.
Now, it turns out the exact number of recursive calls you can make is somewhat dependent on the contents of the routine: for even more Reasons™ every variable in a subroutine (that isn't SHARED, STATIC or a parameter) also takes up an element in the same stack that stores our return locations, and does so on every call. But don't be misled - even a subroutine with no local variables can cause a stack overflow, you just might have to wait a microsecond longer for it to happen.
It can be shown (by some high-pants maths) that every recursive program can actually be written as a nonrecursive one by using one or more loops. Rewriting it to remove the recursion is generally the way to go if you're having stack overflows (and it's not just a bug in your algorithm).