1. Base case & General case
How it works:
- if base case is reached, the loop terminates.(finished)
- else, the general case calls itself with updated parameter again and again until base case is reached
- winding: general case where function calls are getting pushed in the stack
- unwinding: when base case is reached, all data stored in stack gets popped out from top of stack in reversed order
- final output ('Unwinding: 3') happens before stack is completely empty
def recursive_function(n):
if n == 0: # Base case: stop recursion
return
print(f"Winding: {n}") # Executed during stack winding
recursive_function(n - 1) # Recursive call (stack grows)
print(f"Unwinding: {n}") # Executed during stack unwinding
# Call the function
recursive_function(3)
Winding: 3
Winding: 2
Winding: 1
Unwinding: 1
Unwinding: 2
Unwinding: 3
2. Stack
🧠 Techniques Used in the RecursiveCalculate Function
| Technique / Concept |
Code Example |
Purpose / What It Does |
| Default Argument |
tofind = None |
Allows caller to skip providing tofind; used to initialize it only on first call. |
| Initialization on First Call |
if tofind is None: tofind = num |
Sets tofind once (to remember original number); avoids resetting in recursion. |
| Base Case |
if num == 0: return 0 |
Stops recursion; returns 0 so the sum can begin building during unwinding. |
| Recursive Call |
RecursiveCalculate(num - 1, tofind) |
Solves a smaller version of the problem (checking a smaller number). |
| Conditional Logic |
if tofind % num == 0: |
Checks whether num is a divisor of the original number. |
| Building the Result Recursively |
return num + RecursiveCalculate(...) |
Accumulates the result (sum of divisors) during the return phase. |
| Return-based Total |
return num + ... or subtotal += num |
Avoids using external total variable; adds directly through return values. |
| Immutable Constant Value |
Passing tofind unchanged in recursive calls |
Keeps the reference number constant during recursion. |
| No Loops Used |
Pure recursion instead of for or while loops |
Demonstrates a recursive approach to problems typically done with iteration. |
🔁 Optional Variants You Could Add
| Alternative |
How It's Used |
Effect |
| Using an accumulator |
def helper(current, total) |
Tail recursion style; carries the result in a parameter (less Pythonic, but possible). |
| Using an inner helper function |
def helper(num, tofind) |
Encapsulates logic and hides extra parameters from the user. |
Avoiding num + ... inline |
Store result in a variable first (subtotal) |
Makes the logic more readable and debuggable. |
Stack winding, unwind table
| Stack winding phase |
pushed returned value onto stack |
RecursiveCalculate(4,4)
|
return 4 + RecursiveCalculate(3,4)
|
| RecursiveCalculate(3,4) |
return RecursiveCalculate(2,4) |
| RecursiveCalculate(2,4) |
return 2+ RecursiveCalculate(1,4) |
| RecursiveCalculate(1,4) |
return 1 + RecursiveCalculate(0,4) |
| RecursiveCalculate(0,4) |
0 |
Unwinding phase (returns):
- RecursiveCalculate(0,4) returns 0
- RecursiveCalculate(1,4) returns 1 + 0 = 1
- RecursiveCalculate(2,4) returns 2 + 1 = 3
- RecursiveCalculate(3,4) returns 3 (no addition, since 4 % 3 != 0)
- RecursiveCalculate(4,4) returns 4 + 3 = 7 <-- final result
Beautiful version: auto update tofind to permanent value
def Recursive(num, tofind=None):
if tofind is None:
tofind = num
# set permanent value num to tofind
#set tofind to first num,
#otherwise it will be the parameter passed in
#base case
if num ==0:
return 0
#general case
total = Recursive(num-1, tofind)
if tofind % num == 0:
#add to total
total += num
return total
Easy version: mannual update tofind
def Recursivetwo(num, tofind):
#base case
if num == 0:
return 0
#general case
elif tofind % num == 0:
return num + Recursivetwo(num-1, tofind)
#add to total
else:
return Recursivetwo(num-1, tofind)