为什么这个递归程序有效?

sri*_*714 1 c++ recursion

#include <iostream>

using namespace std;

int main() {
    int n,x;
    int fact(int);

    cin >> n;

    x = fact(n);

    cout << x;

    return 0;
}

int fact(int n) {
    if(n!=1)
        return n*fact(n-1);
}
Run Code Online (Sandbox Code Playgroud)

在最后一种情况下,当传递给函数fact的参数fact(1)为1时,如果不在我的代码中指定它,它如何计算等于1?

Log*_*rat 10

该程序依赖于未定义的行为.它肯定不能保证工作,但您可能已经发现了一个情况,即您发送的参数(1)位于调用代码将其视为返回值的位置.难道不是依赖此行为.

许多C++编译器会将此代码拒绝为具有语义问题:并非所有控制路径都从fact()返回值