Understanding Recursion

BackerLeader posted 1 min read

Have you ever seen those Russian dolls, where one doll opens to reveal a smaller one inside, and then another smaller one, and so on? Recursion in programming is a bit like that. It’s when a function calls itself to solve a smaller piece of the problem until it reaches the simplest version, which can be solved directly.

Let’s take a simple real-world example: printing a list of names from last to first.

# Recursive function to print names in reverse order
def print_names_reverse(names, index):
    # Base case: stop when we reach the beginning
    if index < 0:
        return
    
    # Print the current name
    print(names[index])
    
    # Recursive call: move to the previous name
    print_names_reverse(names, index - 1)

# Sample list
names_list = ["Alice", "Bob", "Charlie", "Diana"]

# Start recursion from the last index
print_names_reverse(names_list, len(names_list) - 1)

How it works step by step:

We start at the last name (“Diana”).

The function prints it, then calls itself for the previous name.

This keeps happening until we reach the first name.

Once the first name is printed, the function stops — this stopping point is called the base case.

Notice how the function keeps calling itself, but each time it’s a slightly smaller problem. That’s recursion in its simplest form.

Recursion can look magical, but it’s all about breaking a problem into smaller pieces. Once you get the hang of it, you’ll see it’s a very natural way to solve problems — especially ones that repeat themselves.

Sometimes, recursion can end up repeating the same work for different parts of a problem. This is where a concept called memoization comes in — it’s a clever way to remember results so you don’t redo work unnecessarily.

Curious about making recursion super efficient and avoiding repeated work? Check out my Medium article on recursion with memoization
to see the next level in action! (Free link attached for you)

If you read this far, tweet to the author to show them you care. Tweet a Thanks

Nice article Rahul.
one suggestion is that function to print names in reverse order should not require an additional parameter for index. That should be managed internally by the function.
So, the code may be re-written as follows:

Function to print names in reverse order

def print_names_reverse(names):
    # Recursive function to print names in reverse order
    def print_names_reverse(names, index):
        # Base case: stop when we reach the beginning
        if index < 0:
            return
        
        # Print the current name
        print(names[index])
        
        # Recursive call: move to the previous name
        print_names_reverse(names, index - 1)
    print_names_reverse(names, len(names)-1)

# Sample list
names_list = ["Alice", "Bob", "Charlie", "Diana"]

# Start recursion from the last index
print_names_reverse(names_list)

The whole idea here is that the user of the print_names_reverse should not be required to pass additional parameter.

Agree, Nice adjustment Pravin. However we can do it without nesting.

# Recursive function to print names in reverse order
def print_names_reverse(names, index=None):
    # Initialize index on first call
    if index is None:
        index = len(names) - 1
    
    # Base case: stop when we reach the beginning
    if index < 0:
        return
    
    # Print the current name
    print(names[index])
    
    # Recursive call: move to the previous name
    print_names_reverse(names, index - 1)

# Sample list
names_list = ["Alice", "Bob", "Charlie", "Diana"]

# Now you only pass the names
print_names_reverse(names_list)

Thanks Rahul for the change.
I am basically a java guy and there I prefer to use the recursive method as a private method and the public method is exposed without recursion, where the additional parameter is not specified.
Yes Python has named parameters with default values.

Ahaan... Now I know why you had nested function. I'm familiar with Java syntax. I got it.

More Posts

Building Queue From Stack

rahul mishra - Sep 5

Discovering JavaScript's Hidden Secrets: Understanding Graph Algorithms.

David Green - Sep 1

Discovering JavaScript's Hidden Secrets: Understanding Searching Algorithms.

David Green - Jun 6

Discovering JavaScript's Hidden Secrets: Understanding Algorithms.

David Green - May 28

Why Space & Time Complexity Matters in Your Code ?

rahul mishra - Oct 2
chevron_left