# Please explain this functions declaration to me

• 04-21-2008
Swerve
Please explain this functions declaration to me
Hi,

I have this piece of code, and was hoping someone would so kind as to explain to me in English what each part 'does'.

Code:

```struct Patient {     int id;     double weight;     double height;     bool ismale; }; // new function declaration void WeightLoss(Patient *countWeight);```
It's the part in bold I'm wondering about.

I understand that *countweight is both declaring the pointer 'countWeight' and that it is the argument of the function.

I'm just a little confused on Patient 's role, is it that by including Patient before *countWeight - it means that the pointer *countWeight will point to an element within the struct Patient? Or is it performing some other role?

Many thanks!
• 04-21-2008
anon
It is pointing to a whole Patient instance. Through countWeight you can access the id, weight, height and ismale member of that Patient instance. (The name countWeight sounds a bit misleading though.)
• 04-21-2008
Swerve
Ahh, brilliant!

Within the function's body there is:-

Code:

```void WeightLoss(Patient *countWeight) {     for(int i = 0;i < MAXPATIENTS; i++)     {         if (countWeight[i].ismale == 1)         {             countWeight[i].weight = countWeight[i].weight * 90 / 100;         }         else         {             countWeight[i].weight = countWeight[i].weight * 80 / 100;         }     } }```
So countWeight is kind of replacing Patient, it's like a gateway to the rest of the struct.

Thanks for taking the time to explain it, appreciate it :)
• 04-21-2008
matsp
Quote:

Originally Posted by Swerve
Ahh, brilliant!

Within the function's body there is:-

Code:

```void WeightLoss(Patient *countWeight) {     for(int i = 0;i < MAXPATIENTS; i++)     {         if (countWeight[i].ismale == 1)         {             countWeight[i].weight = countWeight[i].weight * 90 / 100;         }         else         {             countWeight[i].weight = countWeight[i].weight * 80 / 100;         }     } }```
So countWeight is kind of replacing Patient, it's like a gateway to the rest of the struct.

Thanks for taking the time to explain it, appreciate it :)

Well, to be precise, countWeight is a badly named variable of the TYPE Patient.

--
Mats
• 04-21-2008
Swerve
Hi matsp,

Do you mean that the name used isn't a good explanation of what it represents/does.

Like naming var1 var7 when there is only one var. Makes the code difficult to understand, whilst though it doesn't affect how the code works/performs.
• 04-21-2008
matsp
Quote:

Originally Posted by Swerve
Hi matsp,

Do you mean that the name used isn't a good explanation of what it represents/does.

Like naming var1 var7 when there is only one var. Makes the code difficult to understand, whilst though it doesn't affect how the code works/performs.

Yes. A variable called "countWeight", I would expect to contain a number of weights, or some such, not patient data. It's probably a consequence of a change in the code from what it USED TO DO to what it does now.

If you call your variables Fred, countWeight, x, b32aa or xzascajeaeqasexz doesn't matter one bit to what the code does. Their names are to help YOU know what's going on (and anyone else wishing to read and understand the code, of course), but the compiler would be just as happy if all variables are called a1, a2, a3, a4 etc - it makes no odds - it doesn't understand what it means anyways - as long as the names follow C coding standards, it's fine by the compilers view.

--
Mats
• 04-21-2008
Swerve
Sweet, I think I'll change these things now so it makes the code easier to follow and understand.
• 04-21-2008
medievalelks