DEV Community

Philemon Adaghe
Philemon Adaghe

Posted on

What is Recursion?

What is Recursion?

Recursion is when a function calls itself to solve smaller parts of a problem — ideal for tasks that can be broken down into repetitive subproblems.

✅ Basic Recursive Structure

def function_name():

# base condition

if condition:

return something
Enter fullscreen mode Exit fullscreen mode

else:

return function_name() # recursive call
Enter fullscreen mode Exit fullscreen mode

Without a base condition, the function will keep calling itself forever — causing a RecursionError.

💡 Example 1: Factorial Using Recursion

def factorial(n):

if n == 0 or n == 1: # base case

return 1
Enter fullscreen mode Exit fullscreen mode

else:

return n * factorial(n - 1)
Enter fullscreen mode Exit fullscreen mode

print(factorial(5)) # Output: 120

Here’s how it works:

factorial(5)

→ 5 * factorial(4)

→ 5 * 4 * factorial(3)

→ 5 * 4 * 3 * factorial(2)

→ ...

→ 5 * 4 * 3 * 2 * 1 = 120

💡 Example 2: Fibonacci Series Using Recursion

def fibonacci(n):

if n == 0:

return 0
Enter fullscreen mode Exit fullscreen mode

elif n == 1:

return 1
Enter fullscreen mode Exit fullscreen mode

else:

return fibonacci(n-1) + fibonacci(n-2)
Enter fullscreen mode Exit fullscreen mode

for i in range(7):

print(fibonacci(i), end=' ') # Output: 0 1 1 2 3 5 8

🔨 Mini Project:

Factorial & Fibonacci Calculator

def factorial(n):

return 1 if n <= 1 else n * factorial(n - 1)

def fibonacci(n):

if n <= 1:

return n
Enter fullscreen mode Exit fullscreen mode

else:

return fibonacci(n - 1) + fibonacci(n - 2)
Enter fullscreen mode Exit fullscreen mode

num = int(input("Enter a number: "))

print("Factorial:", factorial(num))

print("Fibonacci:", fibonacci(num))

Scale globally with MongoDB Atlas. Try free.

Scale globally with MongoDB Atlas. Try free.

MongoDB Atlas is the global, multi-cloud database for modern apps trusted by developers and enterprises to build, scale, and run cutting-edge applications, with automated scaling, built-in security, and 125+ cloud regions.

Learn More

Top comments (1)

Collapse
 
dotallio profile image
Dotallio

So clear! Recursion always clicks better for me with visual examples - do you use any tools or diagrams to help see the call stack when debugging?

Scale globally with MongoDB Atlas. Try free.

Scale globally with MongoDB Atlas. Try free.

MongoDB Atlas is the global, multi-cloud database for modern apps trusted by developers and enterprises to build, scale, and run cutting-edge applications, with automated scaling, built-in security, and 125+ cloud regions.

Learn More

👋 Kindness is contagious

Explore this insightful piece, celebrated by the caring DEV Community. Programmers from all walks of life are invited to contribute and expand our shared wisdom.

A simple "thank you" can make someone’s day—leave your kudos in the comments below!

On DEV, spreading knowledge paves the way and fortifies our camaraderie. Found this helpful? A brief note of appreciation to the author truly matters.

Let’s Go!