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:
#include <iostream>
usingnamespace 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 ) )
{
returnfalse;
}
}
returntrue;
}
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.
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.
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>
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.