Thread: Advice on how to call a struct function inside another struct function

1. Advice on how to call a struct function inside another struct function

Hi, I have a struct function (struct 1) and I want to call another struct function (struct 2) from struct 1 using input of struct 1 itself. Basically :

• Inside Struct Age, I need to calculate Age. And using conditional statement, if Age requirement is accepted --> Call struct Housing.
• Struct Housing using input from 2 different structs, including struct Age (the caller struct). So I define struct Housing before struct Age.

Code is below :
This is the 1st struct
Code:
```struct Salary salary (struct Hour hours)
{  //some calculation

struct Salary result;
result.S = S;
return result;
}```
This is the 2nd struct
Code:
```struct Housing house (struct Salary sal, struct Age member)
{  //calculation & calling value sal.S (from struct Salary)
//and member.age (from struct Age)

struct Housing result;
result.D = D;
return result;
}```

And this is where I need to call struct inside conditional statement :
Code:
```struct Age pass (struct Hour hours, int x, int y)
{   //some calculation

if (fabs (a2-a1) < 5){
printf("Age requirement - %3.f %3.f : PASS \n",x,y);
struct Age result;
result.age  = age;
result.ret  = TRUE;
return result;

struct Housing H = house(salary, pass); //Error here
//This struct call its own func (pass).
}

//some false statements
}```

However I encountered error :
Code:
```Error : error: passing 'struct Salary (struct Hours)' to parameter of incompatible type 'struct Salary'
struct Housing H = house(salary, pass);
^~~~~
passing argument to parameter 'sal' here```
I hope my explanation isn't confusing. Where did I miss in my code ?

2. You cannot "call" struct objects. Rather, you call functions. In this case, you want to call a function and pass a struct object as an argument:
Code:
`struct Housing H = house(salary(hours), result);`
But you have a return statement before this, so this means that this will never be executed.

Also, it might help if you started naming your functions with verbs instead of nouns, since you seem confused. For example, instead of "house", come up with a descriptive name involving a verb as to what that function does, e.g., computeHousingSomething, where Something is replaced by sonething only you know. Also, stop using names like S and D. Use descriptive names. I would give suggestions, but it is precisely because you failed to be descriptive that I don't know what these are about. Sally and Donovan? Sugar and Donut?

3. Originally Posted by laserlight
You cannot "call" struct objects. Rather, you call functions. In this case, you want to call a function and pass a struct object as an argument:
Code:
`struct Housing H = house(salary(hours), result);`
But you have a return statement before this, so this means that this will never be executed.

Also, it might help if you started naming your functions with verbs instead of nouns, since you seem confused. For example, instead of "house", come up with a descriptive name involving a verb as to what that function does, e.g., computeHousingSomething, where Something is replaced by sonething only you know. Also, stop using names like S and D. Use descriptive names. I would give suggestions, but it is precisely because you failed to be descriptive that I don't know what these are about. Sally and Donovan? Sugar and Donut?
Thank you for your suggestion and advice on naming variable, I'll work on it. Also, if I don't return the value first, how am I supposed to use it as an input for a struct inside the same condition. Maybe I should change the function after all.

4. Originally Posted by pixie_laluna
if I don't return the value first, how am I supposed to use it as an input for a struct inside the same condition.
I think you meant "function" or "scope", not "condition". Anyway, if you return a value from the function, control leaves the function and re-enters the function that called that function. If you want to use a struct object, after assigning to it, just use it.

5. Originally Posted by laserlight
I think you meant "function" or "scope", not "condition". Anyway, if you return a value from the function, control leaves the function and re-enters the function that called that function. If you want to use a struct object, after assigning to it, just use it.
Thank for correction ! English isn't my main language. We usually call function / scope with other name in our language so I wasn't aware. Also, the function that call a function with return value (struct Age) here is my main function. But I can't call the Housing struct in the main as well, because this function doesn't need to be executed all the time. Only if Age returns true.

Maybe I can return the return value of Age and make statement condition in main to check if it's true then I can call Housing. If possible I want to do it inside Age, but as for now I try to check which one works.

6. This is an example of an if statement, as in the whole code is an if statement:
Code:
```if (condition)
{
// body of if statement
// ...
}```

7. Originally Posted by laserlight
This is an example of an if statement, as in the whole code is an if statement:
Code:
```if (condition)
{
// body of if statement
// ...
}```
You're too kind. Even I understand the structure of if condition. Thanks again