The short answer
A recursive function in Python is a function that calls itself. It needs a base case that returns without calling itself, and each call should move closer to that base case. The idea matches the C++ version, but Python has a few details of its own, such as a recursion limit and integers that never overflow.
A factorial example in Python
def factorial(n):
if n <= 1: # base case
return 1
return n * factorial(n - 1) # recursive case
print(factorial(5)) # 120
The base case n <= 1 also covers 0, so factorial(0) returns 1. Each call waits for a smaller call, then multiplies its own n by the result.
How the calls unfold
For factorial(3), the calls go like this:
- factorial(3) waits for factorial(2).
- factorial(2) waits for factorial(1).
- factorial(1) meets the base case and returns 1.
- The waiting calls finish in reverse order: 2 × 1 = 2, then 3 × 2 = 6.
The recursion limit
Python limits how deep the calls can go. In a default CPython install, the limit is 1000, and you can check it with sys.getrecursionlimit():
import sys
print(sys.getrecursionlimit()) # 1000 by default in CPython
try:
factorial(5000)
except RecursionError:
print("The recursion went too deep.")
The call factorial(5000) needs far more than 1000 nested calls, so Python raises a RecursionError. Python does not remove tail calls, so a deep recursion cannot be relied on to run. The function sys.setrecursionlimit can raise the limit, but setting it too high can crash the interpreter. For deep counting, a loop avoids the problem.
Integers in Python and C++
Python integers grow as needed, so factorial(25) returns an exact result with no overflow. A C++ int is a fixed-size type, typically 32 bits, and 13! is already too large for it. Signed integer overflow is undefined behavior in C++, so the usual fix is a wider type such as long long, which holds 20! but not 21!.
The same idea in C++
Here is the same function in C++ with a 64-bit integer type:
#include <iostream>
long long factorial(long long n) {
if (n <= 1) { // base case
return 1;
}
return n * factorial(n - 1); // recursive case
}
int main() {
std::cout << factorial(20) << "\n"; // 2432902008176640000
return 0;
}
The Python and C++ versions have the same structure: a base case, a recursive case, and results that return up the chain. The differences are in the details. C++ requires declared types and has no recursion limit of this kind, but the stack size still limits how deep calls can go. Python checks the depth for you and raises an error.
When to use recursion in Python
Recursion fits Python well when the depth stays modest and the code reads clearly, such as walking nested data. For long counts or very deep inputs, a for loop is safer because it does not add a call for each step.
Summary
A recursive function in Python calls itself until a base case stops it. It works the same way as the C++ version, with a recursion limit that suits problems where the depth stays small. Use the base case to keep the calls finite, and check the limit whenever the input may be large.