I'm sure it can be done and I just need someone to show me how, please.
I have an int function which I use to shuffle values in an int array.
I call this first, shuffling function, from withing another function. In this other function I want to catch the return value of the shuffling function, which returns 0 or 1 based on some specifics of the shuffle method.
Show me please how it would look.
Inside the shuffling function I would place return0; inside one if() block and
return1; inside another, right? (BTW Would I still need to have yet another
return statement, return0;, at the very end of the function?)
How would I catch the return value (either 0 or 1) in the function from which I'm
calling the shuffling function? I need to do it this way because I call the
shuffling function repeatedly, while sending to it different values every time. And I need to be able to determine which set of values to send with the next call based on the result of the previous call to this same function.
Would something like this be permissible?
(I'm doing it in C)
int main()
{
int player = 0;
int my_function(int player)
{
if (something) return1;
if (something_else) return0;
}
void my_other_function (int player)
{
do{
player%2;
if( my_function (int player)) player++;
else fprint ("You get to go again");
}while(something)
}
}
BTW Where do I define function inside of another function? Do you mean in main?
And, in my_fynction, the condition I'm looking for will evaluate to either 0 or 1.
What do I need a final return statement for. The will be no chance of third outcome!
Ok. Got it. I understand where to put it now. Declare in a header - define before/after main. But what about the code itself? If I call my_other_function from main, will the expression inside if(my_function (int player)) parenthesis in my_other_function evaluate
correctly to 0 or 1?
In other words, will the result of a call to my_function, if(from here) look like this?
What do I need a final return statement for. The will be no chance of third outcome!
The code I referred to looked like this:
1 2 3 4 5 6 7
int my_function(int player)
{
if (something)
return1;
if (something_else)
return0;
}
If there is truly no chance of a third outcome it should look like this:
1 2 3 4 5 6 7
int my_function(int player)
{
if (something)
return 1;
elsereturn 0;
}
Or like this:
1 2 3 4 5 6 7
int my_function(int player)
{
if (something)
return 1;
return 0;
}
If there is no final return statement, and no else for the last if, as far as the compiler is concerned, you might have overlooked something.
And remember, the compiler is there to help you. Any errors and warning messages it outputs aren't provided just as obstacles to climb over. On the contrary, they are there to help you write correct, error-free code.