Since both
::playerChoice() and
::compChoice() return
void (in this context, it means
nothing), it cannot return anything; the compiler won't allow it. Since both functions store the choice as an
int, it would be logical to return an
int from both functions. So, both functions should look something like:
1 2 3 4 5 6 7 8 9 10
|
int playerChoice();
// ...
int playerChoice()
{
int Choice(0); // A
// ...
return(Choice);
}
|
Now, how do you use the values the functions returned? It's quite simple. When you call
::winner(), for each parameter of
::winner(), call
::playerChoice() and
::compChoice(). For instance:
1 2 3 4 5
|
int main()
{
//...
::winner(::playerChoice(), ::compChoice()); // B
}
|
In this code, when
::winner() was called, the two other functions were called to. The values returned by both functions were given to the corresponding parameter of
::winner(). I'll dumb it down:
1 2 3 4 5 6 7 8 9
|
int Function_a() { return(0); }
int Function_b() { return(1); }
void Function_c(int A, int B) { }
int main()
{
Function_c(Function_a(), Function_b());
}
|
Before
Function_c() begins, both
Function_a() and
Function_b() are called. When
Function_a() finishes, the compiler places the value it returned into
Function_c()'s
A parameter. When
Function_b() finishes, the compiler places the value it returned into
Function_c()'s
B parameter. When both functions are finished,
Function_c() begins. At this point,
A and
B contain 0 and 1, respectively.
Additionally, data returned from a function can also be placed within variables, like so:
1 2 3 4 5 6 7
|
int Function() { return(0); }
int main()
{
int My_int = Function();
// My_int now equals zero.
}
|
Notes:
A) The parentheses ((...)) used in this way is called functional notation. It's equivalent to
int a = 0.
B) The double colon proceeding a name indicates that the function/variable is global.
Wazzak