1

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

John F.
  • 67
  • 3
  • 9

1 Answers1

4

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.

Mayank Verma
  • 633
  • 6
  • 19
  • good job showing the source of the problem and linking to articles for further reading. Really appreciate making the OP work for it. However you should probably at least hint to a solution. In this case knowing the cause of the problem doesn't help with solving it. He now has to "return" 2 values from the function. – bolov Feb 07 '16 at 07:19
  • He doesn't need to "return" 2 values if he decides to declare a global variable. Not recommended, but it'll work for this particular program. – Mayank Verma Feb 07 '16 at 07:33