C++ Using Output Parameters


Example

Parameters can be used for returning one or more values; those parameters are required to be non-const pointers or references.

References:

void calculate(int a, int b, int& c, int& d, int& e, int& f) {
    c = a + b;
    d = a - b;
    e = a * b;
    f = a / b;
}

Pointers:

void calculate(int a, int b, int* c, int* d, int* e, int* f) {
    *c = a + b;
    *d = a - b;
    *e = a * b;
    *f = a / b;
}

Some libraries or frameworks use an empty 'OUT' #define to make it abundantly obvious which parameters are output parameters in the function signature. This has no functional impact, and will be compiled out, but makes the function signature a bit clearer;

#define OUT

void calculate(int a, int b, OUT int& c) {
    c = a + b;
}