Tail recursion in python-By GolashBoy
Tail recursion in python A recursive function is tail-recursive when the recursive call is the last thing executed by the function i.e the function returns a call to itself. Why care about tail-recursion?🤔 The tail-recursive functions considered better than naive functions as tail-recursion can be optimized by the compiler. The idea is simple since the recursive call is the last statement, there is nothing left to do in the current function, so saving the current function’s stack frame is of no use hence discard everything saved so far in the memory stack. Most programming languages are tail-recursive, which means that they're able to make optimizations to functions that return the result of calling themselves Almost all recursive functions can be transformed into the tail-call form. Here's an example of a function : First in the original form, then transformed into the tail-call form. COPY # Original Function def function ( n ): if n == 0 : r...