简体   繁体   English

如何从函数的循环中调用值?

[英]How to call a value from a loop in a function?

Just need some tutorial on calling a function. 只需一些有关调用函数的教程。 I'm using a loop in it but I have not been taught the overall structure of functions/function calls. 我在其中使用循环,但尚未了解函数/函数调用的整体结构。 Any basic guidance would be great!! 任何基本的指导将是伟大的!

It seems you're a bit confused about the scope of local/global variables. 您似乎对局部/全局变量的范围有些困惑。 The i you declared in main() function is different from the i declared in find_div() function. i你在声明main()函数是从不同的i在宣布find_div()函数。 Time to read about local variables , global variables and variable shadowing . 是时候阅读有关局部变量全局变量变量阴影的信息了 With this knowledge I hope you'll be able to solve your problem. 有了这些知识,我希望您能够解决您的问题。 Come back to me if you've any doubts, but you have to show you've at least tried. 如果您有任何疑问,请回到我身边,但您必须证明自己至少已尝试过。

EDIT: Consider the code snippet below: 编辑:考虑以下代码片段:

int find_div(int num) {
    int i;

    for (i = 2; i <= (num/2); i++) {
        if (num % i == 0) {
            return 1;
        }
        if (num == i) {
            return 0;   //This line never executes.
        }
    }
    return i; //Think what this does to your program.
}

Read the comment in the snippet. 阅读摘要中的评论。 There is a logical error. 存在逻辑错误。

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

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