# Thread: Arguments and parameters help.

1. ## Arguments and parameters help.

Is there anyway we can return 2 values from a called function
example
Code:
```float xxxx(float a, float b, float c, float d)
{///
///
///
}

void xxx()
{
int e,f,g,h;
////
////
xxx(e,f,g,h);
}```
so if I want for example a+b and c+d, can i return those 2 answer? I don't think its possible since I am new into C programming

2. It's possible. You can pack as many return values as you want in a structure, for example.

3. Originally Posted by AFCKING
Is there anyway we can return 2 values from a called function

Code:
`float xxxx(float a, float b, float c, float d);`
A simple way is to declare some "out parameters" in your function signature. For example, you would change the above to the following

Code:
```void xxxx(float a, float b, float c, float d, float *resultA, float *resultB) {
*resultA = ...;
*resultB = ...;
}```
Then you can call your function like this

Code:
```float a,b;
xxxx(1.2f, 3.4f, 5.6f, 7.8f, &a, &b);  // save results in a and b```

4. Don't use it myself, but couldn't something like below be used or have I forgotten how the mechanism works? I'm curious.

Code:
```float* xxxx(float a, float b, float c, float d)
{
float *tmp = malloc(sizeof(float) * 2);

if(tmp)
{
*tmp = a + b;
*(tmp + 1) = c + d;
return tmp;
}

return NULL;
}```
main

Code:
```float *result = xxxx(1, 2, 3, 4);

if(result)
{
printf("a+b = %0.0f, c+d = %0.0f", *result, *(result + 1));
free(result);
}```

5. Originally Posted by ronin
Don't use it myself, but couldn't something like below be used or have I forgotten how the mechanism works? I'm curious.

Code:
```float* xxxx(float a, float b, float c, float d)
{
float *tmp = malloc(sizeof(float) * 2);```
There's nothing wrong with it, except that you might forget to free. Probably the most failure-tolerant way is the suggestion from whiteflags, to make a struct. This has the advantage that you can name what the parameters are for. For example, say you have function foo that returns the sum and the product. Then you could write the function like this

Code:
```typedef struct FOO_RESULT FOO_RESULT;
struct FOO_RESULT {
float sum;
float product;
};

FOO_RESULT foo(float a, float b, float c, float d) {
float x = a + b;
float y = c * d;

return (FOO_RESULT) {
.sum = x,
.product = y,
};
}```
The caller of the function can do so in a way that is "self-documenting" and with no chance of memory allocation violations

Code:
```FOO_RESULT res = foo(1.2f, 3.4f, 5.6f, 7.8f);
printf("Results are: %.2f, %.2f\n", res.sum, res.product);```

6. Thanks c99; it's been a long time since I've tried that method, and wasn't sure if I had it right. I'd go with the struct too, but pass it as a parameter.