/*****************************/ /* Example of a good routine */ /* By: Brien Beattie */ /*****************************/ /* Required header information */ #include char *DigitName(int TheDigit); // Function prototype /* A main program to test the routine */ /* This program prints out the numbers one to ten (as words) */ void main() { for (int i=0; i<10; i++) cout << DigitName(i) << endl; } /* This example routine is a function to return the word */ /* correspoinding to a specified integer in the range 1 to 9 */ char *DigitName(int TheDigit) { char *Result; // defines a pointer to be returned switch (TheDigit) { case 0: Result = new char[5]; strcpy(Result, "Zero"); break; case 1: Result = new char[4]; strcpy(Result, "One"); break; case 2: Result = new char[4]; strcpy(Result, "Two"); break; case 3: Result = new char[6]; strcpy(Result, "Three"); break; case 4: Result = new char[5]; strcpy(Result, "Four"); break; case 5: Result = new char[5]; strcpy(Result, "Five"); break; case 6: Result = new char[4]; strcpy(Result, "Six"); break; case 7: Result = new char[6]; strcpy(Result, "Seven"); break; case 8: Result = new char[6]; strcpy(Result, "Eight"); break; case 9: Result = new char[5]; strcpy(Result, "Nine"); break; default: *Result = char(0); } return Result; } /**********************************************************************/ /* This routine performes one simple task. */ /* - It has functional cohesion */ /* By using this routine, a programmer can avoid duplicating code. */ /* This routine is general enough to be used in any program. */ /* - It is portable. */ /* - It is easy to update. */ /* - It hides an event sequence from programers. */ /* - It promotes code re-use. */ /* This routine uses no global data. */ /* - It is intimate. */ /* This routine is not too big. */ /**********************************************************************/