Skip to content

Commit d0e9b35

Browse files
authored
Updated recursion.md with example to explain the working of recursion with simple python program
This commit updates the recursion explanation in the recursion.md file to be more beginner-friendly and comprehensive. The factorial example has been revised with n = 4 and stack variables have been included for each recursive call to demonstrate how functions are called and how values are returned. These changes aim to provide a clearer understanding of recursion, especially for beginners, by illustrating the step-by-step process of function calls and return values.
1 parent 368955a commit d0e9b35

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

contrib/ds-algorithms/recursion.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,49 @@ When a recursive function is called, the following sequence of events occurs:
1717
- Stack Management: Each recursive call is placed on the call stack. The stack keeps track of each function call, its argument, and the point to return to once the call completes.
1818
- Unwinding the Stack: When the base case is eventually met, the function returns a value, and the stack starts unwinding, returning values to previous function calls until the initial call is resolved.
1919

20+
# Python Code: Factorial using Recursion
21+
22+
```python
23+
def fact(n):
24+
if n == 0 or n == 1:
25+
return 1
26+
return n * fact(n - 1)
27+
28+
if __name__ == "__main__":
29+
n = int(input("Enter a positive number: "))
30+
print("Factorial of", n, "is", fact(n))
31+
```
32+
33+
## Explanation
34+
35+
This Python script calculates the factorial of a given number using recursion.
36+
37+
- **Function `fact(n)`:**
38+
- The function takes an integer `n` as input and calculates its factorial.
39+
- It checks if `n` is 0 or 1. If so, it returns 1 (since the factorial of 0 and 1 is 1).
40+
- Otherwise, it returns `n * fact(n - 1)`, which means it recursively calls itself with `n - 1` until it reaches either 0 or 1.
41+
42+
- **Main Section:**
43+
- The main section prompts the user to enter a positive number.
44+
- It then calls the `fact` function with the input number and prints the result.
45+
46+
### Example : Let n = 4
47+
48+
49+
The recursion unfolds as follows:
50+
1. When `fact(4)` is called, it computes `4 * fact(3)`.
51+
2. Inside `fact(3)`, it computes `3 * fact(2)`.
52+
3. Inside `fact(2)`, it computes `2 * fact(1)`.
53+
4. `fact(1)` returns 1 (`if` statement executes), which is received by `fact(2)`, resulting in `2 * 1` i.e. `2`.
54+
5. Back to `fact(3)`, it receives the value from `fact(2)`, giving `3 * 2` i.e. `6`.
55+
6. `fact(4)` receives the value from `fact(3)`, resulting in `4 * 6` i.e. `24`.
56+
7. Finally, `fact(4)` returns 24 to the main function.
57+
58+
59+
#### So, the result is 24.
60+
61+
62+
2063
# What is Stack Overflow in Recursion
2164

2265
Stack overflow is an error that occurs when the call stack memory limit is exceeded. During execution of recursion calls they are simultaneously stored in a recursion stack waiting for the recursive function to be completed. Without a base case, the function would call itself indefinitely, leading to a stack overflow.

0 commit comments

Comments
 (0)