Using global variables:
Code:
int x, y, z;
void f();
int main(void)
{
f();
printf("f() returned %d, %d, %d via globals\n",x,y,z);
return 0;
}
void f()
{
x = 3;
y = 4;
z = 5;
}
Above, the function f() effectively returns values by changing global variables. This is the stupidest method possible.
Using a struct:
Code:
struct foo { int x, y, z; };
struct foo f();
int main(void)
{
struct foo bar;
bar = f();
printf("f() returned %d, %d, %d via a struct\n",bar.x, bar.y, bar.z);
return 0;
}
struct foo f()
{
struct foo t;
t.x = 3; t.y = 4; t.z = 5;
return t;
}
Above, f() returns a struct, containing 3 fields.
Using pointers:
Code:
void f(int *, int *, int *);
int main(void)
{
int a, b, c;
f(&a, &b, &c);
printf("f() returned 3 values using pointers: %d, %d, %d\n", a,b,c);
return 0;
}
void f(int *x, int *y, int *z)
{
*x = 3; *y = 4; *z = 5;
}
Above, f() is passed the addressed of each variable in main, and sets them by dereferencing.