# can I have two returns?

Printable View

• 02-26-2002
Dummies102
can I have two returns?
can I have an inted function return two values? thanks
• 02-26-2002
Govtcheez
Functions can only return 1 value... You can return a pointer to a struct with lots of values in it, though.
• 02-26-2002
Dual-Catfish
:D

int theFunction(int num1, int num2)
{

int *ptrReturn = new int[2];
ptrReturn[0] = (num1 + num2) / 2;
if (num1 < num2)
{
ptrReturn[1] = num2;
} else {
ptrReturn[1] = num2;
}

return ptrReturn;
}

<Meanwhile, outside the function>
int avgPlusHighest = theFunction(2, 3);
// avgPlusHighest[0] == Average;
// avgPlusHighest[1] == Highest number;

Someone correct me if I'm wrong :)
• 02-26-2002
Stoned_Coder
That works but you have to remeber to free the memory used after you are finished with it. It is often better to pass an extra variable or two in by reference if your function needs to return more than one value.That way the changes to the values in the function actually makes changes to the values in the calling function.
• 02-26-2002
Unregistered
Or just pass an array to the function and let it be filled by the function.
• 02-26-2002
Dummies102
thanks all
• 02-26-2002
Dummies102
well... here's the function:

int PythSimp(int i, int one, int two, int a)
{
one = one * one;
two = two * two;
one = one + two;
for (i=1;one % i !=0;i++)
{
two=one/i;
}

return two;
}

I want this function to tell me what two is and what i is. is there a way to do this with this type of function?