How does incrementing the variable count by the recursive call to the function, save count from being reset to zero everytime it is invoked?
I am trying to get a firm understanding of how recursion works.
def countSubStringMatchRecursive(target,key):
"""Searches for instances of the key substring occuring inside the target string.
Returns the count of all occurences of key in target."""
count = 0 # Count of occurences found.
found = -1 # Stores result of search
i = 0 # Index into target to begin searching.
strTarget = str(target) # Insure that target is a string type.
strKey = str(key) # Insure that key is a string type.
# Guard against empty target or key.
if strKey == '' or strTarget == '':
return 0
if len(strTarget) >= len(strKey):
found = strTarget.find(strKey)
if found > -1:
count += 1
count += countSubStringMatchRecursive(strTarget[found + len(strKey):], strKey)
return count