Need help comprehending simple code.

Hello all, I need some help comprehending how this code works. I'm learning C++ with Jumping Into C++ and I'm struggling immensely with some of the example codes he gives, despite his thorough walking through of topics, I can't wrap my head around how some things work. For example, this program that prints prime numbers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

using namespace std;

// note the use of function prototypes
bool isDivisible (int number, int divisor);
bool isPrime (int number);

int main ()
{
    for ( int i = 0; i < 100; i++ )
    {
    if ( isPrime( i ) )
    {
    cout << i << endl;
    }
    }
}

bool isPrime (int number)
{
    for ( int i = 2; i < number; i++)
    {
    if ( isDivisible( number, i ) )
    {
    return false;
    }
    }
    return true;
}

bool isDivisible (int number, int divisor)
{
return number % divisor == 0;
}


How does this program get a value for "number" and "divisor." From what I understand, isPrime can only take an integer value in its parameters when called. However, this program calls 'i' which is defined as nothing outside of a condition for loops. It was my understanding that functions can't use the variables from other functions. isDivisable takes two values, both integers, but when the function is called both its parameters don't use an integer value. I have NO idea how the values for "number" and "divisor" are defined. Clearly I have a huge misunderstanding of how this works, and despite a friend trying very very hard to explain it to me, I can't understand how the values are defined.

Any help would be greatly appreciated, thanks.
However, this program calls 'i' which is defined as nothing outside of a condition for loops.

isPrime() is called "inside" the for loop though so its value depends on how many iterations of the loop have past. It is the same for isDivisable() and number == the original i value.

If you set a break point at the start of the for loop in main and step though it, it will be easier to visualize what is happening.
Are you saying that the values for isPrime()/isDivisible() are being read from 'i's definition in the for loop? Am I mistaken?

What I mean is... on line 13:

if ( isPrime(i) )

Is the value for i read as 0, because the for loop's condition is int i = 0?

If this is true, how does "divisor" and "number" get a value? I can accept that logic for i, but the other two are a mystery.
Last edited on
Basically, yes and also number in the case of isDivisable().

Like I said, try stepping through it with a debugger. If you use VS, its simple. CB takes a little more work since you have to tell it what variables to display. I'm not sure how other options might work other than adding debug lines, but I'd lower the iterations quite a bit in main() if you go that route.

<edit>
Once either of the loops end, that i (or number) goes out of scope and isn't available anymore like you were thinking originally though.
</edit>
Last edited on
I have to ask again, because I don't fully understand. I get that the isPrime/isDivisible gets its value from the 'i' defined in the for loop... but neither number nor divisor are defined in any for loop or prior to any loops, so how do they get their value?
isDivisible() is getting the values from the for loop in isPrime().
1
2
3
for ( int i = 2; i < number; i++)
    {
    if ( isDivisible( number, i ) )

number is the value received from i in the main loop.

Stepping through it: the first time through isPrime() gets a 0 that is assigned to number making the condition in the loop
 
for ( int i = 2; i < number0; i++)

so the loop is skipped and it returns true. The same thing happens the next two iterations for 1 and 2. The fourth time, isPrime() gets a 3 making the condition
 
for ( int i = 2; i < number3; i++)

so the if statement is entered and isDiviible() is called.
 
return number3 % divisor2 == 0;
Topic archived. No new replies allowed.