简体   繁体   English

C function 在 return 语句之后重复 if 语句

[英]C function repeats if statement after return statement

Does anyone know if a function is supposed to end after it returns something?有谁知道 function 返回后是否应该结束? I have my recursion function written below, but whenever it reaches the else statement and after it returns a value (steps),我的递归 function 写在下面,但是每当它到达 else 语句并在它返回一个值(步骤)之后,

it runs the "if (new_number % 2 == 1)" statement,它运行“if (new_number % 2 == 1)”语句,

which does not make sense since the function should end when it reaches the else statement and should not repeat.这是没有意义的,因为 function 应该在到达 else 语句时结束并且不应重复。

It works fine until it returns "steps" for the first time.它工作正常,直到它第一次返回“步骤”。

This is what happens after the first return: It doesn't even fully run the "if (new_number % 2 == 1)" statement, it just jumps to that line and decreases the value of "steps" and "input_steps" by 1. "new_number" and "number" just get completely random values这是第一次返回后发生的事情:它甚至没有完全运行“if (new_number % 2 == 1)”语句,它只是跳转到那一行并将“steps”和“input_steps”的值减 1 . "new_number" 和 "number" 只是得到完全随机的值

Then it returns "steps", then it jumps to "if (new_number % 2 == 1)" statement and decreases the value of "steps" and "input_steps" by 1. "new_number" and "number" just get completely random values again.然后它返回“steps”,然后跳转到“if (new_number % 2 == 1)”语句并将“steps”和“input_steps”的值减1。“new_number”和“number”只是得到完全随机的值再次。

It repeats that cycle until "new_steps" and "steps" equal 0, then it returns 0 (because "steps" = 0) and ends the function.它重复该循环,直到“new_steps”和“steps”等于 0,然后它返回 0(因为“steps”= 0)并结束 function。

Does anyone know why it does this????有谁知道为什么会这样????

Here is my code:这是我的代码:

int step_recursion(int number, int input_steps)
{
    int new_number = number;
    int steps = input_steps;

    if (new_number != 1)
    {
        if (new_number % 2 == 0)
        {
            if (new_number != 1)
            {
                step_recursion(new_number / 2, steps + 1);
            }
        }
        if ((new_number % 2) == 1)
        {
            if (new_number != 1)
            {
                step_recursion(new_number * 3 + 1, steps + 1);
            }
        }
    }

    return steps;
}

I was expecting the function to end after returning "steps," but for some reason it doesn't.我期待 function 在返回“步骤”后结束,但由于某种原因它没有。 I already described the problem fully so go read that.我已经完整地描述了这个问题,所以 go 阅读了它。

As far as I can see you are trying to implement the famous "Collatz conjecture".据我所知,您正在尝试实施著名的“Collatz 猜想”。 Below is a working version...下面是一个工作版本...

int step_recursion(int number, int steps) {
   if (number == 1) {
      return steps;
   }

   if (number % 2 == 0) {
      return step_recursion(number / 2, steps + 1);
   } else {
      return step_recursion(number * 3 + 1, steps + 1);
   }

   return steps;
}
  • In your code you are checking twice if the number is not equal to 1. Its better if you have an "early return"在您的代码中,如果数字不等于 1,您将检查两次。如果您有“早退”则更好
  • You don't return the value of the recursion calls, you are just calling the function您不返回递归调用的值,您只是调用 function

Let's go inline for a moment, then we'll cover your question , which seems excessively verbose and difficult to digest (and I'll explain that when I come back to it).让我们 go 内联一会儿,然后我们将讨论您的问题,该问题似乎过于冗长且难以消化(我会在回过头来解释)。 Note the comments I added to this excerpt of your code...请注意我添加到您的代码摘录中的注释...

if (new_number % 2 == 0)
{
    if (new_number != 1)
    {
        // if your intent is to control flow such that execution doesn't continue beyond here, you'd surely want a `return` statement here...
        /* return step_recursion(new_number / 2, steps + 1); */
        step_recursion(new_number / 2, steps + 1);
    }
}
if ((new_number % 2) == 1)
{
    if (new_number != 1)
    {
        /* ditto here */
        step_recursion(new_number * 3 + 1, steps + 1);
    }
}

I already described the problem fully so go read that.我已经完整地描述了这个问题,所以 go 阅读了它。

My issue with your question is that it's rather lengthy, full of erroneous premises (which are kind of questions disguised as erroneous statements of fact) and unnecessary (English) boilerplate, which ought to be asked as different questions.我对你的问题的看法是,它相当冗长,充满了错误的前提(这些问题被伪装成错误的事实陈述)和不必要的(英语)样板文件,应该作为不同的问题提出。 For example:例如:

Which book are you reading that teaches you "the function should end when it reaches the else statement"?您正在阅读哪本书教您“function 应该在到达 else 语句时结束”? This is a fundamental misunderstanding of C. Just to be clear, the else statement doesn't cause a function to return .这是对 C 的根本误解。需要说明的是, else语句不会导致 function 返回

It works fine until it returns "steps" for the first time.它工作正常,直到它第一次返回“步骤”。

In other words, "it works fine until it doesn't work", which is an entirely superfluous statement that we could attach to almost any question on this.network.换句话说,“它工作正常直到它不工作”,这是一个完全多余的声明,我们可以附加到 this.network 上的几乎任何问题。 That doesn't add meaning, though, does it?但是,这并没有增加意义,不是吗? Hopefully your future questions are easier to digest, with less of this meaningless fluff.希望你以后的问题更容易理解,少一些毫无意义的废话。

Does anyone know why it does this????有谁知道为什么会这样????

The absence of a full problem description isn't the only reason for closure;缺少完整的问题描述并不是关闭的唯一原因; sometimes we close questions because they have multiple built-in questions ... The answer to this question is yes , but that single word answer doesn't help you understand. 有时我们会关闭问题,因为它们有多个内置问题......这个问题的答案是肯定的,但那个单一的答案并不能帮助你理解。

Does anyone know if a function is supposed to end after it returns something?有谁知道 function 返回后是否应该结束?

... and to be clear, asking if something is a different question to asking why something. ... 需要明确的是,询问是否某事与询问为什么某事是不同的问题。 The answer to this different question is also yes .这个不同问题的答案也是肯定的。

TLDR; TLDR; take this with a pinch of salt, but when you come here you really need to think about what information you're asking for and how to ask for that exact information without asking other questions (or injecting invalid assertions) at the same time.对此持保留态度,但是当您来到这里时,您确实需要考虑您要询问的信息以及如何在不询问其他问题(或注入无效断言)的情况下询问准确的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM