为什么递归函数的输出为0?

Max*_*itj -2 c recursion

有人可以解释一下为什么这段代码返回0?

#include <stdio.h>
int factorial(int input)
{
    if (input > 0)
    {
        input--;
        return input * factorial(input);
    }
    return 1;
}
int main()
{
    printf("%d", factorial(20));

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Sou*_*osh 7

对于最后一次入站迭代,在代码中,当input为1时,执行

if (input > 0)
    {
    input--;  // see here, 1 goes to 0.....
    return input * factorial(input);
    }
Run Code Online (Sandbox Code Playgroud)

基本上给你

  return 0 * factorial (0);
Run Code Online (Sandbox Code Playgroud)

最终将整个返回值设为0.