I think you aren't understanding the use of references as parameters. The result is that the value of "radius" is never set; I will explain why.
When a variable is declared, it is declared within a
scope, which can conceptually be seen as a "context". A scope can be a function, or a class, or global, or anything. The scope defines the visibility of a variable. If I do this:
1 2 3 4 5 6 7 8 9 10
|
// Variable "myInt" is declared in myFunction() and used in main().
void myFunction() {
int myInt = 0; // Variable declared
}
int main() {
myFunction();
myInt += 1; // Error! "myInt" undefined in Main.
return 0;
}
|
So Main doesn't know about myInt, because it only exists in the scope (context) of myFunction. Similarly, if I do this:
1 2 3 4 5 6 7 8 9
|
// Variable "myInt" is declared in main() and used in myFunction().
void myFunction() {
myInt += 1; // Error! "myInt" undefined in myFunction()!
}
int main() {
int myInt = 0;
myFunction();
return 0;
}
|
So what you
can do, is pass variables from one scope to a lower one by using it as a parameter.
1 2 3 4 5 6 7 8
|
void myFunction(int myParameter) {
myParameter += 1;
}
int main() {
int myInt = 0;
myFunction(myInt); // Pass myInt as myParameter.
return 0;
}
|
Now myFunction can use myParameter. However, literally passing a variable will cause the function to
make a local copy of the variable in the scope of that function! Basically, behind the scenes it does this:
1 2 3 4
|
void myFunction(int myParameter) {
int myLocalCopy = myParameter;
myLocalCopy += 1;
}
|
This means myInt is never changed, because the function doesn't actually have access to it; just the local copy! In fact, none of the myFunction() definitions above will have any effect, because only local changes are made and never fed back into the calling scope (here: main).
There are two main ways of making a function (a "called" scope) influence the caller (the "calling" scope). One is by returning a value.
1 2 3 4 5 6 7 8 9 10
|
void myFunction(int myParameter) {
myParameter += 1;
return myParameter;
}
int main() {
int myInt = 0;
myInt = myFunction(myInt);
return 0;
}
|
This is useful and is in many cases the easiest/best/fastest/sexiest way of having a function communicate back to the calling scope. However, sometimes the situation is more complex. There are many possible reasons, but lets just say that you need a function to return 2 values instead of just one. C++ doesn't support multiple return values. However, you can give a function the permission to change the original variable that is passed to it as a parameter, by passing that variable
by reference.
1 2 3 4 5 6 7 8 9 10 11 12
|
void myFunction(int& myFirstParameter, int& mySecondParameter) {
myFirstParameter += 1;
mySecondParameter -= 1;
}
int main() {
int myFirstInt = 0;
int mySecondInt = 10;
myFunction(myFirstInt, mySecondInt);
// Values are now 1 and 9 respectively!
return 0;
}
|
Now see if you can apply this to your code. The problem variable is "radius", which is not being read in and therefore keeps its default value.
[edit] In case it's not clear: the "reference" definition comes by adding the ampersand in the parameter. Notice the 'int' versus 'int&'. 'int' parameters are passed by value (= copied), 'int&' parameters are passed by reference (= changeable [unless const]).
Also, as @And G said, you can pass variables by pointer. The logic is the same, but the syntax is messier. References are [in my opinion] a much cleaner way of getting this behavior than pointers. I think a solid rule for C++ beginners is "if you can do it by reference, do it by reference; otherwise use pointers".