Forget about this "borrow" thing. There's no borrowing going on -- you might be just confusing yourself.
Think of a variable as a small box. It holds a single value:
Here we have a box named 'foo', and inside that box is the number 5.
Now we are creating another box named 'bar'. Inside that box, we take whatever value is inside foo and
copy it so it is also inside bar. So now, both our foo and bar boxes contain the number 5.
Now we take whatever is inside foo (5), subtract it by 1, and put that value back in foo.
So now our foo box contains a value of 4, and our bar box still contains a value of 5.
Parameters are the same as variables. Only instead of assigning them with the = operator, the assignment is implicit:
1 2 3 4 5 6 7 8 9 10
|
void func(int param)
{
cout << param;
}
int main()
{
int foo = 6;
func(foo);
}
|
Here, main() has a box named foo that has 6 in it.
Then, when it calls the 'func' function, it is giving the contents of foo as a parameter. This means that the contents of the 'foo' variable is being copied into the contents of our 'param' variable in func.
So now.. both func's 'param' box
and main's 'foo' box both contain the number 6.
Then when func sends that value to cout, a "6" is printed on screen.
So now to answer your question more directly:
how does " if ( isDivisible( number, i ) ) " on line 24 get its value for "number?" |
"number" is the parameter. It gets its value from whatever was passed in when the function was called. In your case... main() is calling it. Take a look at line 13:
main is giving the contents of its 'i' box as the parameter. Therefore, isPrime's 'number' box will contain the same value as whatever was inside of main's 'i' box at the time of the call.