Write a recursive function in python

Okay, here is an example of a Python function written recursively to implement factorial calculation:

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)

In this function, we first check nif the given number is 0. If yes, return 1. Otherwise, we return the result of ntimes factorial(n-1). Since this function calls itself repeatedly, we get recursion.

Guess you like

Origin blog.csdn.net/weixin_35756624/article/details/129553281