Recursion in Python (factorial function)

前端 未结 2 1798
礼貌的吻别
礼貌的吻别 2021-01-28 20:48

I hope this is not too much of a stupid question, but why does the \'return 1\' statement in this Python code return the factorial of a number? This also happens for \'return Tr

2条回答
  •  不要未来只要你来
    2021-01-28 21:46

    n == 0 is the base case of the recursive function. Factorial of 0 is 1: reference

    Once the base case returns 1, the statement return n * factorial(n-1) will have the form: return n * 1 and so on.

提交回复
热议问题