Re: Help with C++

From: jmh (j_m_h_at_cox.net)
Date: 07/23/04

  • Next message: Mike Wahler: "Re: Order in the STL 'set'?"
    Date: Thu, 22 Jul 2004 19:37:16 -0400
    
    

    Mike Bower wrote:
    > #include <iostream.h>
    >
    >
    >
    > // Function prototype
    >
    > void calcFatInfo(int, int, int &, double &);

    This just says the function is not going to return anything
    so don't try using it to assign some value to a variable --
    which is somewhat incorrect because the last two parameters
    are references (that's the & sign's signifigance).

    > int main( )
    >
    > {
    >
    > int totalCal = 0;
    >
    > int fatGrams = 0;
    >
    > int fatCal = 0;
    >
    > double fatPercent = 0.0;
    >
    >
    >
    > cout <<"Enter the total calories: ";
    >
    > cin >> totalCal;
    >
    > cout <<"Enter the grams of fat: ";
    >
    > cin >> fatGrams;
    >
    > if (totalCal >= 0 && fatGrams >= 0)
    >
    > {
    >
    > calcFatInfo(totalCal, fatGrams, fatCal, fatPercent);
    >
    > cout << "Fat calories: " << fatCal << endl;
    >
    > cout << "Fat percentage: " << fatPercent << endl;
    >
    > }
    >
    > else
    >
    > {
    >
    > cout << "Input error";
    >
    > }
    >
    > return 0;
    >
    > }
    >
    >
    >
    > void calcFatInfo(int tCal, int grams, int &fCal, double &fPer)
    >
    > {
    >
    > fCal = grams * 9;
    >
    > fPer = 100.0* fCal / tCal;

    Here fCal and fPer get assigned the calculated values. Since
    the two arguments are references (again the &) the function
    is actually putting the calculated value into the memory
    location of fatCal and fatPercent which were declared in
    main--effectively returning values to the caller (main).

    At least this is how I understand it and I'm speaking as
    one beginer to another.

    jmh


  • Next message: Mike Wahler: "Re: Order in the STL 'set'?"