Yes and No and Yes again. . o O (Maybe it is No, and yes, and yes again).
In general a function returns 1 thing. I can't have
int Sum(int x, int y) return a double, or two integers.
Problem #1: returning two integers: To do this you would need to combine the integers into a structure.
CODE
typedef struct {
int x;
int y;
} Coordinate;
Coordinate Translate(int xShift, int yShift, Coordinate p);
int main()
{
Coordinate Point;
Point.x=0; Point.y=0;
Translate(5, 5, Point);
cout << Point.x << ", " << Point.y << ".\n";
return 0;
}
Coordinate Translate(int xShift, int yShift, Coordinate p)
{
p.x +=xShift;
p.y +=yShift;
return p;
}
I could also have returned a pointer to an array (this is the way I ussualy do things in assembly language).
Another way to do this is to pass the function referances to the variables I want to change.
CODE
void func(int &x, int &y);
void main()
{
int i=0;
int j=0;
func(i);
cout << i <<'\n';
return 0
}
void func(int &x, int &y) { x++;y++; return; }
Then there is the other question: Can I get a function to return either a double or an integer? We can do this with overloading, however the functions can not differ in only the return value, the call values must also be different.
CODE
int func(int a);
double func(double a);
int func(int a) { return 2 * a; }
double func(double a) { return 2 * a; }
We can even go a step farther and make a template function:
CODE
template<class T> T Max(T a, T b) { return ( a > b) ? a : b; }
int main ()
{
int a = 5, b=4, c=0;
double d = 12, e= 2, f=0;
c = Max(a,b);
f = Max(e, d);
return 0;
}
I am SURE that there are at least 2 more ways to do one, the other, or both of these things. If you would like to know more about a particular method, ask away.