Please explain this functions declaration to me

This is a discussion on Please explain this functions declaration to me within the C++ Programming forums, part of the General Programming Boards category; Hi, I have this piece of code, and was hoping someone would so kind as to explain to me in ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    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!

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    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

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Swerve View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    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.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Swerve View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Sweet, I think I'll change these things now so it makes the code easier to follow and understand.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    I'd download and start reading before doing much more coding...

    http://www.mindview.net/Books/TICPP/...ngInCPP2e.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. Plz explain difference between functions returning ref
    By dudeomanodude in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2008, 01:03 PM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  4. Attaching functions to a class/struct
    By VirtualAce in forum Tech Board
    Replies: 2
    Last Post: 08-04-2003, 10:56 AM
  5. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 09:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21