# Thread: How to correctly call struct return as input for other struct functions ?

1. ## How to correctly call struct return as input for other struct functions ?

Hi, I want to pass the return value of function A to function B, and pass the return value of function B to function C. I'm using struct. I can pass from struct to function and then main, but turn out I'm still confuse how to pass struct return to a function which return another struct as well.

Here's how I define my struct

Code:
```struct Credit {
float A;
float B;
float C;
};

struct Intermediate {
float math;
float bio;
};```
Then I have my first function, with manual input value from the user. And from this function I return the value of A, B and C

Code:
```struct Credit course (float x1, float y1, float x2, float y2)
{
// simple arithmetic calculation...

struct Credit result;
result.A = A;
result.B = B;
result.C = C;
return result;
}```
In my second function, I need to get input from A,B,C from the previous function. And then return the value of math, bio as input from next function.

Code:
```struct Intermediate grade (struct Credit point)
{
// simple calculation involving return result of previous function (A, B, C)
// I call the value using point.A , point.B and point.C

struct Intermediate result;
result.math = math;
result.bio = bio;
return result;
//result from this function will be used for the next function
}```
Here's my main :
Code:
```
course(12,10,10,8);
//next function using input from function grade.
```
When I put the first function as input of the second function, it expected input (indeed the first function needs input) but I wanted to call the return value not the function. I'm confuse. Any advice ? Thank you

2. Either will do.
Code:
```    // assign as you go
struct Credit c = course(12,10,10,8);

// merge calls
//next function using input from function grade.
But you're not saving anything by squashing everything together.
Given grade(course(12,10,10,8)), the compiler is going to create an invisible struct Credit temp to make it look as if you split things out.

3. Returning or taking structures as arguments to a function, by value, is valid. But "by value" puts some pressure on the stack and tends to create less optimized code. Usually is more efficient to use pointers... For exemple:
Code:
```struct S
{
float a, b, c, d;
};

// returning the structure
struct S f ( float x1, float y1, float x2, float y2 )
{
struct S result;

result.a = x1;
result.b = y1;
result.c = x2;
result.d = y2;

return result;
}

void g ( struct S *resultptr, float x1, float y1, float x2, float y2 )
{
resultptr->a = x1;
resultptr->b = y1;
resultptr->c = x2;
resultptr->d = y2;
}```
The increase in complexity of g() has its advantages:

1. If your structure is big, only a pointer is used as argument, i.e, the structure contents will not be copied to stack. This means less stack usage;
2. You can keep only one instance of structure object on memory all times...

Depending on the level of optimization and if your structure is short enough, these functions are almost identical. Try to compile the example with and without optimizations enabled and compare the created code in assembly.

[]s
Fred

4. Originally Posted by flp1969
Returning or taking structures as arguments to a function, by value, is valid. But "by value" puts some pressure on the stack and tends to create less optimized code. Usually is more efficient to use pointers... For exemple:
Code:
```struct S
{
float a, b, c, d;
};

// returning the structure
struct S f ( float x1, float y1, float x2, float y2 )
{
struct S result;

result.a = x1;
result.b = y1;
result.c = x2;
result.d = y2;

return result;
}

void g ( struct S *resultptr, float x1, float y1, float x2, float y2 )
{
resultptr->a = x1;
resultptr->b = y1;
resultptr->c = x2;
resultptr->d = y2;
}```
The increase in complexity of g() has its advantages:

1. If your structure is big, only a pointer is used as argument, i.e, the structure contents will not be copied to stack. This means less stack usage;
2. You can keep only one instance of structure object on memory all times...

Depending on the level of optimization and if your structure is short enough, these functions are almost identical. Try to compile the example with and without optimizations enabled and compare the created code in assembly.

[]s
Fred
Hi, thanks !
I was aware I can use pointer as well. But to be honest I think I still don't fully understand pointer. For pointer, I passed the written exam but failed at coding exam. That's why I choose to use struct and I'm not working on complex data so as of now memory won't be a problem, but I'm working on understanding more about pointer. I'll try the example, thanks again !

5. Originally Posted by Salem
Either will do.
Code:
```    // assign as you go
struct Credit c = course(12,10,10,8);