Using Recursion/Recursive Functions in Python, with Examples

Recursive Functions in Python

This article will explain how to use recursion in Python functions, with some example code to illustrate the concept.

recursive function is a function which calls itself one or more times until a condition is met.

If a function that uses recursion calls itself too many times, Python will throw an error.

As this is a demonstration of a code structure rather than anything with any specific syntax, I’ll show a simple example of a recursive function and leave comments explaining what is going on.

Python Recursion Example & Explanation

This example Python code illustrates a recursive function and includes some commentary explaining things:

# Defines a recursive function
# A single parameter is accepted - the count of how many times this function should call itself
def my_recursive_function(count):

    # Define a variable to hold the result which is the current number of remaining times for the function to recur
    result = count -  1

    # If the result is greater than 0, there are still remaining recursions to run
    if(result >= 0):
        print(result)

        # You could put any code here you want to be executed with each recursion

        my_recursive_function(count - 1) # The function is calling itself for the next recursion.  It is subtracting one from the count so that it doesn't recur infinitely

    else:
        # The recursion count has been reached, exit the function by returning the result
        return result

print("Recursive Function Results:")
my_recursive_function(7) # This is where the function is called for the first time - from there it calls itself

Which will output:

Recursive Function Results:
6
5
4
3
2
1
0

In the above example, boolean operators are used to compare values in an if/else statement.

Recursive functions are not limited to updating variables within them and can update global variables for later use.

What Happens When There’s Too Much Recursion in Python

If your function calls itself too many times, you’ll see the following error generated by Python:

RecursionError: maximum recursion depth exceeded

By default, Python has a recursion limit of 1000.

If you find yourself having to use recursion to the degree that hits this limit, you may want to consider restructuring your code rather than raising that limit. Otherwise, you will find yourself running into performance issues.

One way to achieve this is to split your data into small chunks and deal with it in batches.

Nobody likes software that eats up all of their CPU/RAM.

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

Your email address will not be published. Required fields are marked *