尾递归,下面的片段尾递归吗?

Tail recursion, is the follow snippet tail recursive?

我正在学习尾递归,在我交这个问题之前,我想知道代码片段是否是尾递归的是/否答案。

int fib_in(int n, int current, int prev) {
    if (n == 1 || n == 2) { // n = 1 or 2 both gives fib number 1
        return current;
    }
    return fib_in(n - 1, current + prev, current); // recursive call, current gets updated and new prev is the current, so were going backwards if that makes sense
}

int fib(int n) {
   return fib_in(n, 1, 1); // 1 and 1 is the 2 first fib numbers so theyll be used as base cases
}

int main(void) {
    int n, f;
    printf("the nth number: ");
    scanf("%d", &n);

    // call fib function
    f = fib(n);
    printf("%d \n", f);
    return 0;
}

我绝对认为是的,但在我们学习尾递归之前,我做了这个函数是为了响应另一项家庭作业,所以我会使用一种我不知道存在的技术。这就是为什么我有点困惑。

对,就是尾递归。 fib_in() 调用自身并且在返回之前不执行任何额外的计算。

This answer 很好地解释了什么是尾递归。特别注意:

In tail recursion, you perform your calculations first, and then you execute the recursive call, passing the results of your current step to the next recursive step. This results in the last statement being in the form of (return (recursive-function params)). Basically, the return value of any given recursive step is the same as the return value of the next recursive call.

再看看你的代码。您的代码符合此描述吗?