Gowtham SB
www.linkedin.com/in/sbgowtham/ Instagram - @dataengineeringtamil
🎬 Python Guide: Recursive Functions
✅ What is a Recursive Function?
A recursive function is a function that calls itself
in order to solve a smaller version of the same problem.
🎯 You must always define a base case (stopping point) to prevent infinite looping.
🔁 Real-Life Analogy
Imagine you're going up stairs:
Each time you take one step, you say:
“One step done... ask the same question for the rest.”
🔧 Simple Example: Factorial
5! = 5 × 4 × 3 × 2 × 1 = 120
✅ Recursive Code:
def factorial(n):
if n == 1:
return 1 # base case
return n * factorial(n - 1) # recursive step
🔹 Usage:
print(factorial(5)) # 120
🧠 Execution Flow:
factorial(5)
→ 5 * factorial(4)
Gowtham SB
www.linkedin.com/in/sbgowtham/ Instagram - @dataengineeringtamil
→ 5 * 4 * factorial(3)
→ 5 * 4 * 3 * factorial(2)
→ 5 * 4 * 3 * 2 * factorial(1)
→ 5 * 4 * 3 * 2 * 1 = 120
🔁 Another Simple Example: Countdown
def countdown(n):
if n == 0:
print("🧨 Boom!")
return
print(n)
countdown(n - 1)
countdown(5)
# Output: 5 4 3 2 1 🧨 Boom!
❌ Common Mistake: No base case = Infinite
recursion
def oops(n):
return oops(n - 1) # ❌ No stopping point
Will raise:
RecursionError: maximum recursion depth exceeded
🔥 When to Use Recursive Functions
Problem Type Example
Math problems Factorial, Fibonacci
Tree/graph data Navigating folder structure, trees
Divide & conquer Binary search, merge sort
Gowtham SB
www.linkedin.com/in/sbgowtham/ Instagram - @dataengineeringtamil
Nested structures JSON traversal, HTML DOM
📌 TL;DR (1-liner):
A recursive function solves a problem by calling itself
with a smaller input, and always includes a base case to stop.
🎯 Challenge for Your Audience (Reel Idea):
# Print numbers from 1 to N using recursion
def print_numbers(n):
if n == 0:
return
print_numbers(n - 1)
print(n)
print_numbers(5)
# Output: 1 2 3 4 5
About the Author
Gowtham SB
www.linkedin.com/in/sbgowtham/ Instagram - @dataengineeringtamil
Gowtham SB is a Data Engineering expert, educator, and content creator with a
passion for big data technologies, as well as cloud and Gen AI . With years of
experience in the field, he has worked extensively with cloud platforms, distributed
systems, and data pipelines, helping professionals and aspiring engineers master the
art of data engineering.
Beyond his technical expertise, Gowtham is a renowned mentor and speaker, sharing
his insights through engaging content on YouTube and LinkedIn. He has built one of
the largest Tamil Data Engineering communities, guiding thousands of learners to
excel in their careers.
Through his deep industry knowledge and hands-on approach, Gowtham continues to
bridge the gap between learning and real-world implementation, empowering
individuals to build scalable, high-performance data solutions.
𝐒𝐨𝐜𝐢𝐚𝐥𝐬
𝐘𝐨𝐮𝐓𝐮𝐛𝐞 - https://www.youtube.com/@dataengineeringvideos
𝐈𝐧𝐬𝐭𝐚𝐠𝐫𝐚𝐦 - https://instagram.com/dataengineeringtamil
𝐈𝐧𝐬𝐭𝐚𝐠𝐫𝐚𝐦 - https://instagram.com/thedatatech.in
𝐂𝐨𝐧𝐧𝐞𝐜𝐭 𝐟𝐨𝐫 𝟏:𝟏 - https://topmate.io/dataengineering/
𝐋𝐢𝐧𝐤𝐞𝐝𝐈𝐧 - https://www.linkedin.com/in/sbgowtham/
𝐖𝐞𝐛𝐬𝐢𝐭𝐞 - https://codewithgowtham.blogspot.com
𝐆𝐢𝐭𝐇𝐮𝐛 - http://github.com/Gowthamdataengineer
𝐖𝐡𝐚𝐭𝐬 𝐀𝐩𝐩 - https://lnkd.in/g5JrHw8q
𝐄𝐦𝐚𝐢𝐥 - atozknowledge.com@gmail.com
𝐀𝐥𝐥 𝐌𝐲 𝐒𝐨𝐜𝐢𝐚𝐥𝐬 - https://lnkd.in/gf8k3aCH