Open In App

Recursion in Python

Last Updated : 20 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Recursion involves a function calling itself directly or indirectly to solve a problem by breaking it down into simpler and more manageable parts. In Python, recursion is widely used for tasks that can be divided into identical subtasks.

In Python, a recursive function is defined like any other function, but it includes a call to itself. The syntax and structure of a recursive function follow the typical function definition in Python, with the addition of one or more conditions that lead to the function calling itself.

Basic Example of Recursion:

Python
def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)

print(factorial(5))  

Output
120

Explanation: The factorial of a number n (denoted as n!) is the product of all positive integers less than or equal to n. The recursive approach involves the function calling itself with a decremented value of n until it reaches the base case of 1.

Let’s understand recursion in python deeply:

Basic Structure of Recursive Function

def recursive_function(parameters):

if base_case_condition:

return base_result

else:

return recursive_function(modified_parameters)

img

Base Case and Recursive Case

  • Base Case: This is the condition under which the recursion stops. It is crucial to prevent infinite loops and to ensure that each recursive call reduces the problem in some manner. In the factorial example, the base case is n == 1.
  • Recursive Case: This is the part of the function that includes the call to itself. It must eventually lead to the base case. In the factorial example, the recursive case is return n * factorial(n-1).

Example:

Python
def fibonacci(n):
    # Base cases
    if n == 0:
        return 0
    elif n == 1:
        return 1
    # Recursive case
    else:
        return fibonacci(n-1) + fibonacci(n-2)

# Example usage
print(fibonacci(10))  

Output
55

Explanation:

  • Base Cases: If n == 0, the function returns 0. If n == 1, the function returns 1. These two cases are necessary to stop the recursion.
  • Recursive Case: The function calls itself twice with the decrements of n (i.e., fibonacci(n-1) and fibonacci(n-2)), summing the results of these calls. This division into smaller subproblems continues until the base cases are reached.

Types of Recursion in Python

Recursion can be broadly classified into two types: tail recursion and non-tail recursion. The main difference between them is related to what happens after the recursive call.

  • Tail Recursion: This occurs when the recursive call is the last operation executed in the function, with no additional work or calculation following the recursive call. In many programming languages, tail recursion can be optimized by the compiler into iterative loops to improve performance and prevent stack overflow.
  • Non-Tail Recursion: This occurs when there are operations or calculations that follow the recursive call. This type prevents the compiler or interpreter from optimizing the recursion into an iteration.

Here is a Python example that demonstrates both tail recursion and non-tail recursion:

Python
def tail_fact(n, acc=1):
    # Base case
    if n == 0:
        return acc
    # Tail recursive call with an accumulator
    else:
        return tail_fact(n-1, acc * n)

def nontail_fact(n):
    # Base case
    if n == 1:
        return 1
    # Non-tail recursive call because the multiplication happens after the call
    else:
        return n * nontail_fact(n-1)

# Example usage
print(tail_fact(5))  
print(nontail_fact(5))  

Output
120
120

Recursion vs Iteration

Recursion:

  • Recursion is often more intuitive and easier to implement when the problem is naturally recursive, like tree traversals.
  • It can lead to solutions that are easier to understand compared to iterative ones.

Iteration:

  • Iteration involves loops (for, while) to repeat the execution of a block of code.
  • It is generally more memory-efficient as it does not involve multiple stack frames like recursion.

Advantages of using recursion

  • Simplicity: Recursive code is generally simpler and cleaner, especially for problems inherently recursive in nature (e.g., tree traversals, dynamic programming problems).
  • Reduced Code Length: Recursion can reduce the length of the code since the repetitive tasks are handled through repeated function calls.

Disadvantages of using recursion

  • Memory Overhead: Each recursive call adds a new layer to the stack, which can result in significant memory use, especially for deep recursion.
  • Performance Issues: Recursive functions may lead to slower responses due to overheads like function calls and returns.
  • Risk of Stack Overflow: Excessive recursion can lead to a stack overflow error if the recursion depth exceeds the stack limit.


Next Article
Practice Tags :

Similar Reads