re: Explain Recursion Like I'm Five VIEW POST

VIEW FULL DISCUSSION
 

To begin, if you want to understand recursion, you must understand what is recursion.

This explain pretty much the concept of recursion. :D


In the programming world, you can find recursive algorithms, such as sorting ones, and you can guess they aren't looping on themselves infinitely, we always add a return condition to be sure we won't have an infinite loop.

FYI we don't do infinite recursive function because it would pollute the "call stack". This stack keeps track of which function was called, and from where it was called, to be able to get back there when we'll met a return statement.

Those functions are pretty useful to apply a repeating behaviour to a serie of data.

For example :

def bubbleSort(data: list) -> list:
    output = []
    if len(data) > 2:
        middle_element = len(data) // 2
        new_list = bubbleSort(data[:middle_element]) + bubbleSort(data[middle_element:])
        for element in new_list:
            output.append(element)
    else:
        return [min(*data), max(*data)]
    return output

(actually this function isn't working as intended, it was created only to show you the concept of recursion)

code of conduct - report abuse