John F.
John F.

Reputation: 67

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!!

Upvotes: 1

Views: 295

Answers (1)

Mayank Verma
Mayank Verma

Reputation: 643

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. 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.

Upvotes: 4

Related Questions