Catching the return value of a function

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.

Thank you in advance!
Last edited on
post your code please. i dont get the idea from reading your text

maybe this is already what you're looking for:


1
2
3
4
5
6
7
8
9
int my_function()
{
   return 1;
}

int main()
{
   int my_variable = my_function();
}
Last edited on
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)
    }
}
Last edited on
You cannot define one function inside another.
This function:
1
2
3
4
5
6
7
int my_function(int player)
{
    if (something)
        return1;
    if (something_else)
        return0;
}

First, you mean return 1; and return 0;
You still need a final return statement, to catch the condition where neither if is true.

The other function has errors, the overall structure is reasonable, but the code would need correcting.
What exactly is wrong with it?

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!
Last edited on
The two functions that you're defining, my_function and my_other_function, need to be defined outside of the main function.
You've put your function declarations inside another function (int main() is a function).

To declare a function you need to put it on it's own, so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int my_function(int player)
{
    if (something) return 1;
    if (something_else) return 0;
}

void my_other_function (int player)
{
  do{
      player%2;
      if( my_function (int player)) player++;
      else fprint ("You get to go again");
    } while(something);
}

int main()
{
    //some stuff
    return 0;
}


You can call functions inside other functions (e.g. int my_variable = my_function();), but you can't create functions inside other functions.

Also you need a semi-colon after the while(something) because it's a do-while loop.
Last edited on
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?

my_function (int player) = 0
or
my_function (int player) = 1
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;
    else
        return 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.
Topic archived. No new replies allowed.