Is it possible to have a function that that takes and returns multiple variables other than arrays?
Printable View
Is it possible to have a function that that takes and returns multiple variables other than arrays?
Yes, you can pass arguments as references.
Or return a struct / class.
Then there are also standard containers which probably wouldn't count as an array.
A C or C++ function can only "return" one value, declared as the return value in the function prototype. In reality, you can pass arguments as pointers (or by "reference" if you prefer C++ nomenclature) to local variables which will take on a value after the function returns.
Ie:
I'm a little shaky on C++ code but you could do the same using references...Code:
void area(int length, int width, int *area)
{
*area = length * width;
}
I'd think either would work...Code:
void area(int L, int W, int &Area)
{
Area = L * W;
}
SOrry for the C++ code. In C++, the & operator can have a different meaning depending on context.
Just check out the first example...