<p>Is there a preferred way to return multiple values from a C++ function?  For example, imagine a function that divides two integers and returns both the quotient and the remainder.  One way I commonly see is to use reference parameters:</p>

<pre><code>void divide(int dividend, int divisor, int&amp; quotient, int&amp; remainder);
</code></pre>

<p>A variation is to return one value and pass the other through a reference parameter:</p>

<pre><code>int divide(int dividend, int divisor, int&amp; remainder);
</code></pre>

<p>Another way would be to declare a struct to contain all of the results and return that:</p>

<pre><code>struct divide_result {
    int quotient;
    int remainder;
};

divide_result divide(int dividend, int divisor);
</code></pre>

<p>Is one of these ways generally preferred, or are there other suggestions?</p>

<p>Edit: In the real-world code, there may be more than two results.  They may also be of different types.</p>

Alternatives include arrays,
generators
, and
inversion of control
, but none is appropriate here.
Some (e.g. Microsoft in historical Win32) tend to use reference parameters for simplicity, because it's clear who allocates and how it will look on the stack, reduces the proliferation of structures, and allows a separate return value for success.
"Pure" programmers prefer the struct, assuming it
is
the function value (as is the case here), rather than something that's touched incidentally by the function.  If you had a more complicated procedure, or something with state, you'd probably use references (assuming you have a reason for not using a class).