CSCI 152                           Structures and Functions

 

// -------------------- GLOBAL TYPES ----------------------------------

 

 struct Date

 {

    int month;

    int day;

 };

 

// -------------- PROTOTYPES FOR USER-DEFINED FUNCTIONS --------

 

 void PrintDate   (Date bDay);  // pass a struct by value   

 Date GetDate     (void);       // return a struct

 void ChangeDate  (Date & rBd); // pass a struct by reference 

 int  ChangeMonth ();           // return a value to be assigned to a field

 void ChangeDay   (int & day);  // pass a struct field by reference

 void PrintMonth  (int month);  // pass a struct field by value

 

// --------------------------------------------------------------------------

 

 int main ()

 {

    Date birthday;                   // Create a Date structure

 

    birthday = GetDate ();           // function returns a copy of the struct

    PrintDate (birthday);            // pass by value

    ChangeDate (birthday);           // pass by address using a reference

    PrintDate (birthday);            // pass by value

    birthday.month = ChangeMonth();  // name of field must be qualified

    ChangeDay (birthday.day);        // name of field must be qualified

    return 0;

 }

 

 

 Date GetDate (void)

 {

    Date bd;                       

 

    cout << "\nIn what month were you born? --> (1 = Jan, 2 = Feb, ...) ";

    cin >> bd.month;

    cout << "\nOn what day of ";

    PrintMonth (bd.month);

    cout << " were you born? --> ";

    cin >> bd.day;

    return bd;

 }

 

 

 void ChangeDate (Date & rBd)

 {

    cout << "\nIn what month were you born? --> (1 = Jan, 2 = Feb, ...) ";

    cin >> rBd.month;

    cout << "\nOn what day of ";

    PrintMonth (rBd.month);

    cout << " were you born? --> ";

    cin >> rBd.day;

 }


 void PrintDate (Date bDay)         // or  void PrintDate (const Date & bDay)

 {                                  // (prototype would also have to be changed)

    cout << "\nHappy birthday on ";

    PrintMonth (bDay.month);

    cout << " " << bDay.day;

 }

 

 

// These two functions demonstrate passing individual fields in a struct variable as

// parameters (if you don’t need to pass the whole structure).

// When you call the function, you have to qualify the field name to identify the value/address 

// to be passed, but the function sees only a single variable and doesn’t know that it is

// actually part of a structure.  We could just as easily pass to ChangeMonth or ChangeDay

// one value that is an element in an array of ints – the function sees only an int.

 

 

 int ChangeMonth ()               // Return type is not qualified because the return

 {                                // value is not part of a struct here; the return value 

                                                          //  could be assigned to any integer variable

    int month;                 

    cout << "\nIn what month were you born? (1 = Jan, 2 = Feb, ...) ";

    cin >> month;

    return month;

 }

 

 

 void ChangeDay (int & day)       // Note parameter name is not qualified

 {

    cout << "On what day of the month were you born\n";

    cin >> day;

 }

 

 void PrintMonth (int month)  // parameter is not qualified

 {                            // The function doesn't know the int is part of a struct

    switch (month)

    {

      case  1 : cout << "January";   break;

      case  2 : cout << "February";  break;

      case  3 : cout << "March";     break;

      case  4 : cout << "April";     break;

      case  5 : cout << "May";       break;

      case  6 : cout << "June";      break;

      case  7 : cout << "July";      break;

      case  8 : cout << "August";    break;

      case  9 : cout << "September"; break;

      case 10 : cout << "October";   break;

      case 11 : cout << "November";  break;

      case 12 : cout << "December";  break;

      default : cout << "invalid month number";

     }

 }