No, you cannot return multiple values simply by using (x, y)
— this is not a tuple, as you might expect from other languages, but a bracketed use of the comma operator, which will simply evaluate to y
(which will, in turn, be returned by the return
).
If you want to return multiple values, you should create a struct, like so:
#include <stdio.h>
struct coordinate
{
int x;
int y;
};
struct coordinate myfunc(int a, int b)
{
struct coordinate retval = { a + b , a * b };
return retval;
}
int main(void)
{
struct coordinate coord = myfunc(3, 4);
printf("X: %d\n", coord.x);
printf("Y: %d\n", coord.y);
}
This shows the full syntax of using a struct, but you can use typedef if you prefer something like:
typedef struct
{
int x;
int y;
} coordinate;
coordinate myfunc(int a, int b)
{
coordinate retval = { a + b , a * b };
return retval;
}
// ...etc...