Have a parameter in the function signature that gets incremented for each call. When it approaches the maximum depth of the recursion, do something up .
Here is an example of the ruby-ish pseudocode:
def my_recursive_function(current_depth) # do stuff if current_depth >= MAX_RECURSION_LIMIT # throw exception, or output helpful information or return default value else my_recursive_function(current_depth+1) end end
source share