DEV Community

abbazs
abbazs

Posted on

Tutorial on Recursive Functions using Python

Introduction to Recursive Functions

A recursive function is a function that calls itself to solve a problem by breaking it down into smaller, more manageable subproblems. Recursive functions have a base case, which serves as the stopping condition for the recursion, preventing it from going on indefinitely.

In this tutorial, we'll explore the basic concepts of recursive functions and provide simple code recipes to demonstrate their usage.

Key Concepts

  1. Base Case: The base case is the simplest form of the problem that can be directly solved without further recursion. It is the stopping condition for the recursive function.

  2. Recursive Call: Inside the function, there is a call to the same function with a modified input that moves towards the base case. This recursive call breaks down the original problem into smaller subproblems.

Let's explain the base case and the recursive call for each of the code recipes provided earlier

Code Recipe: Recursive Sum of a List

def recursive_sum(lst):
    if len(lst) == 1:  # Base Case
        return lst[0]
    else:
        return lst[0] + recursive_sum(lst[1:])  # Recursive Call
Enter fullscreen mode Exit fullscreen mode
  • Base Case: The base case in this function checks if the length of the list lst is equal to 1. If the list contains only one element, there is no need to perform further recursion, and the function returns that single element as the sum of the list.

  • Recursive Call: If the list contains more than one element, the function calculates the sum of the first element (lst[0]) and the sum of the rest of the list (recursive_sum(lst[1:])). This is the recursive call, where the function calls itself with a modified input (the rest of the list) to solve a smaller subproblem. The recursive call continues until the base case is reached.

Code Recipe: Recursive Factorial

def factorial(n):
    if n == 0:  # Base Case
        return 1
    else:
        return n * factorial(n - 1)  # Recursive Call
Enter fullscreen mode Exit fullscreen mode
  • Base Case: The base case in this function checks if the value of n is equal to 0. If n is 0, the factorial of 0 is 1, and there is no need to perform further recursion. The function returns 1 as the result.

  • Recursive Call: If n is greater than 0, the function calculates the factorial of n by multiplying n with the factorial of n - 1. This is the recursive call, where the function calls itself with a modified input (n - 1) to solve a smaller subproblem. The recursive call continues until the base case is reached.

Code Recipe: Recursive Power Calculation

def power(base, exponent):
    if exponent == 0:  # Base Case
        return 1
    else:
        return base * power(base, exponent - 1)  # Recursive Call
Enter fullscreen mode Exit fullscreen mode
  • Base Case: The base case in this function checks if the value of exponent is equal to 0. If the exponent is 0, any number raised to the power of 0 is 1, and there is no need to perform further recursion. The function returns 1 as the result.

  • Recursive Call: If exponent is greater than 0, the function calculates the power of base raised to the exponent by multiplying base with the power of base raised to exponent - 1. This is the recursive call, where the function calls itself with a modified input (exponent - 1) to solve a smaller subproblem. The recursive call continues until the base case is reached.

In each of these examples, the recursive call is responsible for breaking down the original problem into smaller subproblems until it reaches the base case, at which point the recursion stops and the final result is obtained. The base case ensures that the recursion does not continue indefinitely, providing a stopping condition for the recursive function.

Guidelines for Writing Recursive Functions

  1. Identify the base case and return the result when the base case is reached.
  2. Implement the recursive call, modifying the input parameters to move towards the base case.
  3. Ensure that the recursion moves towards the base case in each recursive call to avoid infinite loops.
  4. Combine the results of subproblems to get the final result in the base case or during backtracking.
  5. Handle edge cases where recursion may not be applicable and provide appropriate handling.

Top comments (0)