Skip to main content

Posts

Showing posts from 2025

Factorial (Python)

Factorial example:    def r_factorial(n):     if n==1:         return n     else:         t=r_factorial(n-1)         t=t*n     return t      print(r_factorial(5))  Why once t=1 the function continue? The reason the recursion continues after reaching n == 1 is because of how recursive function calls work. Let's break it down step by step. Understanding Recursive Execution: Each recursive call pauses execution until the next recursive call returns a value. The recursion only stops when it reaches the base case, which is n == 1. At that point, the function starts returning values back up the recursive stack. Step-by-Step Breakdown: When you call recur_factorial(5), the function calls itself repeatedly, decreasing n each time: 1. r_factorial(5): Calls r_factorial(4), waiting for its result. 2. r_factorial(4):...