Understanding Recursion
Learn how to use recursion to solve problems in programming.
Understanding Recursion
Introduction
This tutorial will guide you through understanding recursion and how to use it to solve problems in programming.
Step 1: Define the Problem
We are given a problem that can be broken down into smaller sub-problems of the same type.
Step 2: Define the Base Case
We will define a base case that stops the recursion.
Step 3: Define the Recursive Case
We will define a recursive case that breaks down the problem into smaller sub-problems.
Step 4: Call the Recursive Function
We will call the recursive function with the smaller sub-problems.
Example Code
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
print(factorial(5)) # Output: 120
Conclusion
In this tutorial, we learned how to use recursion to solve problems in programming.
Ready for more? These paid resources pick up where this lesson leaves off.
Project-based recursion video courses — the perfect paid next step after these free lessons.
Browse on Udemy →Hand-picked recursion books to master the fundamentals offline.
See on Amazon →Some links on this page are affiliate links: we may earn a commission at no extra cost to you. We only recommend tools we believe are genuinely useful.