coder111
coder111

Reputation: 35

Passing values by reference

I understand what passing arguments by reference means. A reference is a construct that allows a user to declare a new name for an object. Moreover, it allows to return multiple values from a function passing the variables in by reference.

For example:

int i = 7;

int& r=i;
r=9;
i=10;
return r;
return i;

However, I didn`t get it to match with my function:

int Search(int depth, board b)
{
  board b = combined();
  int w = 0;
   w = Evaluate(b);
  int score;
  int bestScore = std::numeric_limits<int>::min(); 
  bool bestMove = false;
  if(depth == 0) return w;
  int x=0, int y=0;
  for (int x = 0; x < 9; ++y) {
      for (int y = 0; y < 9; ++y) {
          while (!occupied(x,y));
          make_move(x, y);              
          score = -Search(depth-1,b);       // recursion 
          unMake(b);
          if(score > bestScore) {         
              bestScore = score;
              bestMove = (x,y);
          }
      }
  }
  return  bestScore; 
  return  bestMove;
}

I must surely be missing something unless I can write

int & score = bestScore;

int& (x,y) = bestMove;

return bestScore;
return bestMove;

Hope someone can help with this.

Upvotes: 1

Views: 114

Answers (3)

Anton Poznyakovskiy
Anton Poznyakovskiy

Reputation: 2169

it allows to return multiple values from a function passing the variables in by reference.

No. A function always returns only one value. You can pass arguments by reference and modify them within the function (unless it's a constant reference), this will modify their values. Consider this example:

void Foo (int x)
{
    ++x;
}

void Bar (int &x)
{
    ++x;
}

int x = 0;

Foo (x);
// x is still 0
Bar (x);
// x is now 1

If you want to return multiple objects from a function, use std::tuple (as Richard Hodges proposed below) or define a struct. The former is simpler, but it's relatively easy to mix up the elements of a tuple. The latter has a bit more overhead, but you have clear member names.

As for your case, you need something like

std::tuple <int, bool> Search(int depth, board b)
{
  int bestScore = std::numeric_limits<int>::min(); 
  bool bestMove = false;

  // do stuff

  return  std::make_tuple (bestScore, bestMove);
}

or

struct Best
{
  int Score;
  bool Move;
};

Best Search(int depth, board b)
{
  Best best = { std::numeric_limits<int>::min(), false };

  // do stuff with best.Score and best.Move

  return best;
}

Upvotes: 7

coder111
coder111

Reputation: 35

Ok.Thanks. Something like this:

struct XY {
int score;  
XY;
}

tuple<int, XY> Search(int depth, board b)
{
  board b = combined();
  int w = 0;
  w = Evaluate(b);
  int bestScore = std::numeric_limits<int>::min(); 
  bool bestMove = false;
  if(depth == 0) return w;
  int x=0, int y=0;
  for (int x = 0; x < 9; ++x) {
  for (int y = 0; y < 9; ++y) {
    while (!occupied(x,y));
     make_move(x, y);              
    score = -Search(depth-1,b);       // recursion 
    unMake(b);
    if(score > bestScore) {         
      bestScore = score;
      bestMove = (x,y);
    }
   }
  }

   // return the best move
   return make_tuple(bestScore, bestXY);
}

There must be still some bugs but at least got something together.

Upvotes: 0

Richard Hodges
Richard Hodges

Reputation: 69854

to return multiple values from a function, the conventional and safe way is to use a std::tuple.

It looks like your function wants to return a best score and a best co-ordinate so start with a declaration that looks like this:

// holds an x/y coordinate
struct XY {
 // need to declare stuff here
};

std::tuple<int, XY> Search(...args....)
{
   // perform calculations here
   ...

   // return the best move
   return std::make_tuple(bestScore, bestXY);
}

Upvotes: 2

Related Questions