can I have an inted function return two values? thanks
can I have an inted function return two values? thanks
Functions can only return 1 value... You can return a pointer to a struct with lots of values in it, though.
-Govtcheez
[email protected]
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
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.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
Or just pass an array to the function and let it be filled by the function.
thanks all
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?