Jump to content
  • Advertisement
Sign in to follow this  
sheep19

How can I make a function to return two values?

This topic is 3839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I have this code:
bool checkWin ()
{

}

int main()
{
	bool winsP1, winsPC;

	/*
	I want to return the first as true and the second as false :-
        I don't want to have: winsP1 = checkWin(); winsPC = !checkWin();
	*/
	
	system ("PAUSE");
	return 0;
}

Thanks in advance!!

Share this post


Link to post
Share on other sites
Advertisement
C and C++ can't return two values. You can put them together as an std tuple, or pass them in by reference and then change the values in the function. Or make a structure / class that holds both the values.

Share this post


Link to post
Share on other sites
There are a few options:


  1. Make a pair

    std::pair<bool, bool>checkwin( )
    {
    // this syntax may not be exactly right
    return std::pair<bool, bool> value(true, false);
    }

  2. Make a custom struct

    struct Result
    {
    bool p1win;
    bool p2win;
    };

    Result checkwin( )
    {
    Result res;
    res.p1win = true; res.p2win = false;
    return res;
    }

  3. Pass result as reference parameters and change them

    void checkwin(bool& p1win, bool& p2win)
    {
    p1win = true;
    p2win = false;
    }

  4. Use a data type that encompasses the two you need to return. Obvioulsy not a general strategy like the others, but can be used in this example.

    enum Result
    {
    P1_WIN,
    P2_WIN,
    NOBODY_WINS
    };

    Result checkwin()
    {
    return P1_WIN;
    }

  5. Use a better language :)



Of these, the only one I really don't like is #3.

Share this post


Link to post
Share on other sites
For completeness:


6. Use individual bits to represent player state
   enum Won
{
NONE = 0,
P1 = 1,
P2 = 2
};

unsigned int checkwin()
{
// both win
return P1 | P2;

// player 1 wins
return P1;

// player 2 wins
return P2;

// nobody wins
return NONE;
}

Share this post


Link to post
Share on other sites
you can make like this:


void checkWin(int *ret1,int *ret2) {
(*ret1) = 1234;
(*ret2) = 5678;
};

void main() {
int value1,value2;
checkWin(&value1,&value2);
printf("%d %d values now!",value1,value2);
};

Share this post


Link to post
Share on other sites
Quote:
Original post by sheep19
I don't want to have: winsP1 = checkWin(); winsPC = !checkWin();


Why not? If you write a function that checks if a player (given as an argument) has won, and call it as above, your intentions are much clearer (so if you come back to your code later, it'll take less time to understand) and if you ever want to add more players, you don't have to rewrite that function.

However, if 'winsP1 == !winsPC', then there is no need to call that function twice (or to return two values), as you can simple do the following: 'winsP1 = playerWin(); winsPC = !winsP1;'

Share this post


Link to post
Share on other sites
Provided winsP1 == !winsPC always holds true then I'm completely with Captain P on this.
Don't have the function return both values, have it return just one, then call it once and invert the value for the other variable.

Not only is this clearer to read and understand, it's also easier to code.

Share this post


Link to post
Share on other sites
Quote:
Original post by Captain P
Quote:
Original post by sheep19
I don't want to have: winsP1 = checkWin(); winsPC = !checkWin();


Why not? If you write a function that checks if a player (given as an argument) has won, and call it as above, your intentions are much clearer (so if you come back to your code later, it'll take less time to understand) and if you ever want to add more players, you don't have to rewrite that function.

However, if 'winsP1 == !winsPC', then there is no need to call that function twice (or to return two values), as you can simple do the following: 'winsP1 = playerWin(); winsPC = !winsP1;'


This was an example. I'm doing AI for my TicTacToe game. It doesn't mean that when
the player doesn't win that the computer wins (I check for it between every turn).

I will use the one that wanMaster suggested, as it's what I want. (this is going to be used to test if a node is a winning one. That's why I don't want to pass the values by reference.

Thanks everyone for your replies!

Share this post


Link to post
Share on other sites
I'd create an object pass it to the function by reference then the function can modify the two values that can be accessed by another function.


class ob{
bool winsP1;
bool winsPC;
public:
get_winsP1() return winsP1;
get_winsPC() return winsPC;
set_winsP1(bool x) winsP1 = x;
set_winsPC(bool y) winsPC = y;
}

int function1(ob &ob1){
//set or get winsP1 and winsPC as needed
return 0;
}

int main{
ob ob1;
function1(ob1);
return 0;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!