# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# # Recursion

# "When you write a recursive function, you have to tell it when to stop recursing. That’s why every recursive function has two parts: the base case, and the recursive case. The recursive case is when the function calls itself. The base case is when the function doesn’t call itself again ... so it doesn’t go into an infinite loop."

def countdown(i): 
    print(i)
    if i<= 0:
        return
    else:
        countdown(i-1)


countdown(5)


# ## The call stack with recursion

# factorial function wiwh recursion
def fact(x):
    if x == 1:
        return 1
    else:
        return x * fact(x-1)


fact(5)

# • Recursion is when a function calls itself.  
# • Every recursive function has two cases: the base case and the recursive case.  
# • A stack has two operations: push and pop.  
# • All function calls go onto the call stack.  
# • The call stack can get very large, which takes up a lot of memory.  









StarcoderdataJupyterScriptsDedupFiltered