Question: The backstory.
In VBA in Excel I created a function that calculate the shortest distance between two lines (vectors). This function returns the apparent intersection point, and the actual distance between them.
To make this work I ended up passing out an array, then sorting out what went where afterwards. It works, but is clunky to read and work with.

In C++ i have made a similar function that returns the point in question.

```
struct point3D
{ 
double x,y,z;
}
point3D findIntersect(const vec3& vector1, const vec3& vector2)
{
// Do stuff...
return point;
}

```

The problem:
I want to return the length as well, since it uses parts of the original calculation. However most of the time I only want the point.

Possible solutions I have looked at are:

Write a separate function for the distance. A lot of extra work for when I want both of them.

* Create a custom struct for this one function. *Seems a bit overkill.*
* Return an array like the VBA function did. *Resulting code is not very intuitive, and requires care when using.*
* Using an optional argument as a reference to a variable to store the distance. *Does not work. The compiler does not allow me to change an optional argument.*
* Using an argument list like this `function(const argin1, const argin2, argout&, argout2&)`. *Just ugh! Would require the user to always expect every variable out.*

I have seen examples with pairs, but the they look like they have to be the same data type. I essentially want to return a `point3D`, and a `double`.

Does anyone have a more elegant solution to returning multiple values from a function?

Answer: 
C++ lets you overload the function, i.e. use the same name, but different parameter types. You can return `point3D` as the return value, and make the `length` reference parameter appear "optional" by providing a separate overload:

```
point3D findIntersect(const vec3& vector1, const vec3& vector2) {
    double ignore;
    return findIntersect(vector1, vector2, ignore);
}
point3D findIntersect(const vec3& vector1, const vec3& vector2, double &length) {
    ... // Implementation goes here
}

```

Callers who don't want to get the length back would call the two-argument overload, while the callers who want the length would call the three-argument one. In both cases the implementation is going to be the same, with the two-argument call providing a reference to an ignored variable for the length.
You sure can use [pairs](http://www.cplusplus.com/reference/utility/pair/).

```
std::pair<point3D, double> findIntersect(const vec3& vector1, const vec3& vector2);

```