C++: how to use pass by reference to return multiple values in a method?

1.1k Views Asked by At

I need to return two doubles. I know a little about pass by reference, but I'm not sure how to use it to return multiple. Here's what I've tried so far. I made two reference parameters and added it to the method I hoped would return two values.

#include "stdafx.h" 
#include <iostream>
using namespace std;
int main() {
  double min;
  double max;
  getDistance(766, 981, 328, 609, min, max);
  getDistance(899, 171, 1009, 282, min, max);
  cout << "Minimum distance is "+minDistance+"inches";
  cout << "Maximum distance is "+maxDistance+"inches";
  return 0;
}

void getDistance(int left, int right, int top, int bottom, double& minDistance, double& maxDistance) {
  int height=bottom - top;
  int width=right - left;
  bool isCNN=width !=height;
  if (isCNN) {
    if (width >=215 && height >=281) minDistance=0;
    maxDistance=14;
    else if (width >=124 && height >=167) minDistance=14;
    maxDistance=33.75;
    else if (width >=76 && height >=111) minDistance=33.75;
    maxDistance=53.5;
    else if (width >=56 && height >=94) minDistance=53.5;
    maxDistance=73.25;
    else if (width >=49 && height >=84) minDistance=73.25;
    maxDistance=93;
    else if (width >=41 && height >=71) minDistance=93;
    maxDistance=112.75;
    else if (width >=28 && height >=57) minDistance=132.5;
    maxDistance=172;
    else if (width >=23 && height >=49) minDistance=191.75;
    maxDistance=270.75;
    else minDistance=270.75;
    maxDistance=480;
  }
  else {
    if (width >=330) minDistance=0;
    maxDistance=6.375;
    else if (width >=238) minDistance=6.375;
    maxDistance=16.25;
    else if (width >=168) minDistance=16.25;
    maxDistance=26.125;
    else if (width >=122) minDistance=26.125;
    maxDistance=36;
    else if (width >=108) minDistance=36;
    maxDistance=55.75;
    else if (width >=91) minDistance=55.75;
    maxDistance=75.5;
    else minDistance=75.5;
    maxDistance=144;
  }
  return;
}

values.

3

There are 3 best solutions below

0
On

You can use std::pair<double, double> as the return type. E.g.

std::pair<double, double> foo()
{
   return {10, 20};
}
0
On

If you really want to 'return' two values with pass-by-reference:

void foo(int& x, int& y)
{
    x = 55;
    y = 66;
}

int main()
{
    int x = 0;
    int y = 0;
    foo(x, y);
    if (x == 55 && y == 66)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

Something like this should do. As others have noted, there are many other ways to actually return two values, but here you go.

In the code you submitted, (syntax issues aside), you need to refer to min and max in your main method in the cout statement. In getDistance() with minDistance and maxDistance, you have created aliases to the doubles passed to it, but those aliases do not exist outside the function!

0
On

As others have pointed out, there are other ways of returning 2 values than passing by reference. If you do in fact want to return via reference, what you have written is almost correct. Rather than use the variables 'minDistance' and 'maxDistance' in the main method (where they have not been declared), you should use the 'min' and 'max' variables that you have declared. In addition, you should use '<<' rather than '+' to append the output in the cout call. Also, if you want the output to appear on separate lines (rather than run together), you should end the line with '<< endl'. Finally, as user4581301 has said, the bodies of your 'if' statements do not include both minDistance and maxDistance. You need braces around the statements in order to include both.

Rewriting your code to take these changes into account (but without ensuring the correctness of your 'calculation' in any way) gives:

#include <iostream>

int main(int, char**) {
  double min;
  double max;
  getDistance(766, 981, 328, 609, min, max);
  getDistance(899, 171, 1009, 282, min, max);
  std::cout << "Minimum distance is " << min << "inches" << std::endl;
  std::cout << "Maximum distance is " << max << "inches" << std::endl;
  return 0;
}

void getDistance(int left, int right, int top, int bottom, double& minDistance, double& maxDistance) {
  int height = bottom - top;
  int width = right - left;
  bool isCNN = width !=height;

  if (isCNN) {
    if (width >=215 && height >=281) {
      minDistance=0;
      maxDistance=14;
    } else if (width >=124 && height >=167) {
      minDistance=14;
      maxDistance=33.75;
    } else if (width >=76 && height >=111) {
      minDistance=33.75;
     maxDistance=53.5;
    } else if (width >=56 && height >=94) {
      minDistance=53.5;
      maxDistance=73.25;
    } else if (width >=49 && height >=84) {
      minDistance=73.25;
      maxDistance=93;
    } else if (width >=41 && height >=71) {
      minDistance=93;
      maxDistance=112.75;
    } else if (width >=28 && height >=57) {
      minDistance=132.5;
      maxDistance=172;
    } else if (width >=23 && height >=49) {
      minDistance=191.75;
      maxDistance=270.75;
    } else {
      minDistance=270.75;
      maxDistance=480;
    }
  } else {
    if (width >=330) {
      minDistance=0;
      maxDistance=6.375;
    } else if (width >=238) {
      minDistance=6.375;
      maxDistance=16.25;
    } else if (width >=168) {
      minDistance=16.25;
      maxDistance=26.125;
    } else if (width >=122) {
      minDistance=26.125;
      maxDistance=36;
    } else if (width >=108) {
      minDistance=36;
      maxDistance=55.75;
    } else if (width >=91) {
      minDistance=55.75;
      maxDistance=75.5;
    } else {
      minDistance=75.5;
      maxDistance=144;
    }
  }
  return;
}