【问题标题】:How are return statements executed in C if there are multiple return statements?如果有多个 return 语句,如何在 C 中执行 return 语句?
【发布时间】:2018-11-03 13:06:00
【问题描述】:

根据我对C的了解,在下面的代码中,函数fact中的第一个return语句应该被执行。而是执行 function 中的最后一个 return 语句。代码在这里如何工作?为什么函数fact 中的第一个return 语句没有执行?

数字的阶乘:

#include <stdio.h>
int a = 2, count = 1, fact0 = 1;

int fact(int n) {
    if (n == count) {
        return fact0;//Only this return statement should be executed
    }
    fact0 = fact0 * a;

    a++;
    count++;
    fact(n);
    return 1;//But this return statement is executed , Why?
}

int main() {
    int n;
    setbuf(stdout, NULL);
    printf("Enter the factorial number\n");
    scanf("%d", &n);
    printf("The factorial of the number is %d", fact(n));
    return 0;
}

输出是:

Enter the factorial number
 4
The factorial of the number is 1

【问题讨论】:

  • 欢迎来到 Stack Overflow。请阅读the help pagesthe SO tour、阅读how to ask good questions,以及this question checklist。请了解如何创建Minimal, Complete, and Verifiable Example。最后,请不要在你展示的代码中使用行号,这让我们很难复制代码自己尝试。
  • 确定我会删除行号并阅读您发送的所有指南。
  • 至于你的问题我建议你learn how to debug your programs。一些rubber duck debugging 以及在调试器中逐行单步执行代码应该会很快地向您显示问题。
  • 您的问题的问题是您希望它从第一次返回中返回,但您没有解释为什么您有这种期望 - 因为对我们来说这似乎很明显。
  • @Allan :不,你不正确。但是,对您的问题的任何澄清都应该通过编辑问题来添加,而不是添加很少有人会阅读的评论。在递归调用之前,count 会递增,递归调用将返回并调用第二个 return 语句。第一个 return 将被调用一次(至少是第一次从 main() 调用),之后递归展开。在递归中,您必须有一个不会进一步递归的“最内层”返回。

标签: c return


【解决方案1】:

你有一堆嵌套的函数调用。最里面的返回来自您期望的地方,其他来自另一个 return 语句。

// 1st call, in main
fact(4); // calls fact(4) with count == 2
    fact(4); // calls fact(4) with count == 3
        fact(4); // calls fact(4) with count == 4
        return fact0; // as expected
    return 1;
return 1;

【讨论】:

  • 那为什么输出打印的是 1 而不是 fact0?
  • 因为它打印从它调用的函数返回的值:第一个...返回1。返回fact0 的函数未被main() 调用。
  • @Allan :因为代码被破坏了——但这不是你问的。交换返回语句以更正它。至少部分纠正它 - 它仍然在其他方面被破坏。
  • @pmg @Clifford 所以main() 只获得它首先调用的函数的return 语句。你们救了我的命。谢谢@pmg@clifford
【解决方案2】:

您需要学习如何调试代码。一个好的开始是像这样修改fact

int fact(int n) {
    printf("a: %d count: %d fact0: %d n: %d\n", a, count, fact0, n);
    if (n == count) {
        printf("n is equal to count\n");
        return fact0;//Only this return statement should be executed                                               
    }
    fact0 = fact0 * a;

    a++;
    count++;
    fact(n);
    return 1;//But this return statement is executed , Why?                                                        
}

然后,运行将如下所示:

$ ./a.out 
Enter the factorial number
4
a: 2 count: 1 fact0: 1 n: 4
a: 3 count: 2 fact0: 2 n: 4
a: 4 count: 3 fact0: 6 n: 4
a: 5 count: 4 fact0: 24 n: 4
n is equal to count
The factorial of the number is 1

这应该可以很好地了解正在发生的事情。如果需要,插入更多打印语句。

【讨论】:

  • @Allan 正如我在帖子开头所说的那样。你需要学习如何调试你的代码。这是我能给你的最好建议。如果需要,引入更多打印输出并尝试用笔和纸执行代码。
【解决方案3】:

问题在于,对于像 4 这样的值,您会输入一些递归级别。

一旦所有子调用完成,程序就会继续执行最后一个 return 语句,无论如何都会给你 1。

一种可行的方法是返回 fact(n) 而不是仅仅调用它并删除最后一个 return 语句。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 2017-02-05
    • 2019-09-04
    相关资源
    最近更新 更多