function which can't return keeps returning value (not garbage value) c++

huangapple go评论49阅读模式
英文:

function which can't return keeps returning value (not garbage value) c++

问题

我在编写一个递归函数时犯了一个错误,该函数的目的是在C++中获取一个Integer32中的最大斐波那契值。

我犯的错误是:

int get_largest_in_fibonacci(int before, int current){
    if(current < 0){
        return before;
    }else{
        get_largest_in_fibonacci(current, before + current);
        // 应该是
        // return get_largest_in_fibonacci(current, before + current);
    }
}

我没有在else语句中返回值。

但在这个问题中,这不是我想知道的。

int main(){
    cout << "result : " << get_largest_in_fibonacci(1,1) << '\n';
    return 0;
}

在这个main()函数中,结果是 result : 2。但我不明白2是从哪里来的。

因为这个表现不佳的函数无法返回一个值。

起初,我以为参数 (1,1) 会在某个地方得到2。

所以我改变了它们的值,比如 (3,5),但结果仍然相同。

然后我想也许那可能是一个垃圾值。

但我认为垃圾值几乎总是会给我随机的"大"数,但在这里并非如此。

后来我稍微改变了main(),像这样:

int main(){
    int result = get_largest_in_fibonacci(1,1);
    cout << "result : " << result << '\n';
    return 0;
}

只是将函数的结果赋给了 int result

但这一次,结果是 result : 0

所以我想知道 get_largest_in_fibonacci 如何首次返回值,以及通过将其分配给int变量来改变返回值如何变化?

英文:

I made a mistake when I made a recursion function whose purpose is get a largest fibonacci value in Integer32 in c++.

The mistake I made was

int get_largest_in_fibonacci(int before, int current){
    if(current &lt; 0){
        return before;
    }else{
        get_largest_in_fibonacci(current, before + current);
        // should have been 
        // return get_largest_in_fibonacci(current, before + current);
    }
}

I didn't return in the else statement.

But In this question that's not I want to know.

int main(){
    cout &lt;&lt; &quot;result : &quot; &lt;&lt; get_largest_in_fibonacci(1,1) &lt;&lt; &#39;\n&#39;;
    return 0;
}

In this main(), The result was result : 2 . But I can't understand where does 2 come from.

Because, That badly behaving function can't return a value.

At first, I thought the argument (1,1) would have been able to 2 in somewhere.

So I changed their value like (3,5) but the result was same.

And then I thought maybe that could be a garbage value.

But I think garbage value almost always keep giving me random "big" number, but that was not the case in here.

And after I changed the main() little bit like this.

int main(){
    int result = get_largest_in_fibonacci(1,1);
    cout &lt;&lt; &quot;result : &quot; &lt;&lt; result &lt;&lt; &#39;\n&#39;;
    return 0;
}

Just assigned the function result to int result.

But In this time, the result was result : 0 .

So I wonder how did the get_largest_in_fibonacci returned in the first place, and how does return value changed by just assigning it to int variable?

答案1

得分: 3

你的假设是错误的。一个不返回void类型的函数,在执行路径上未能return会在运行时表现出未定义的行为。在那一点上一切皆有可能。程序可能会崩溃,返回2或其他任何内容,或者删除你的主目录。

gcc默认会发出一个警告,应该注意,就像所有的警告一样:

警告:控制到达了非void函数的末尾 [-Wreturn-type]

英文:

Your assumption is wrong. A function not returning type void that fails to return in an execution path exhibits undefined behaviour if that path is taken during runtime. At that point all bets are off. The program may crash, return 2 or anything else or delete your home folder.

gcc by default emits a warning, that should be heeded, as all warnings should:

> warning: control reaches end of non-void function [-Wreturn-type]

huangapple
  • 本文由 发表于 2023年6月27日 19:09:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定