Solving The Kempner Function In Python - Why Is This Concurrent Function Continuing After The Base Case Is Reached?
Solution 1:
Recursive calls are just like any other function call: when they return, they return control back to whatever called them.
Say you have a series of numbered recursive calls:
1->2->3->4
Base Case Reached
If recursive call 3 called recursive call 4, and recursive call 4 ended at the base case, returning from recursive call 4 will take you back to recursive call 3, because 3 called 4. This is just like any other functions call:
defsecond_func():
print("Inner")
return5deffirst_func():
return second_func()
When you return from second_func
, you return control back to first_func
, since first_func
called second_func
. You don't immediately exit from second_func
back to main
or something else. It's the same with recursive calls. The only difference when dealing with recursion is first_func
and second_func
are the same function, but that doesn't affect the mechanics of returning.
There is no way (other than using something like exceptions) to exit from the entire call chain at once.
Post a Comment for "Solving The Kempner Function In Python - Why Is This Concurrent Function Continuing After The Base Case Is Reached?"