What does the last line, return 1 + …. do ? How can you return 1 plus a function call?
Below is the assignments text:
These functions recursively count the number of instances of the key in the target string
def countSubStringMatchRecursive(target, key):
currentPosition = find(target, key)
if find(target, key) == -1:
return 0
else:
return 1 + countSubStringMatchRecursive(target[currentPosition+1:], key)
The last line isn’t returning “1 plus a function call”, it is returning 1 + the return value of the function, which is either
0or1depending on whether the condition has been met.It is recursive, in that the return value from the function call will be 1 + the return value of another function call — again, and again, and again, until
find(target, key) == -1.Think of it more like:
return 1 + ( 1 + (1 + (1 + (0))))