|
|
|
|
|
|
|
// Precondition:
// 1 <= month <= 12
// && dayOfMonth is in valid range for the month
// && year is assigned
// Postcondition:
// Function value == day number in the range 1 365
// (or 1 366 for a leap year)
{
int correction; // Correction factor to account for leap year
// and months of different lengths
// Test for leap year
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
{
if (month >= 3) // If date is after February 29
correction =1; // then add one for leap year
}
else
correction = 0;
// Correct for different length months
if (month == 3)
correction = correction - 1;
else if (month == 2 || month == 6 || month == 7)
correction = correction + 1;
else if (month == 8)
correction = correction + 2;
else if (month == 9 || month == 10)
correction = correction + 3;
else if (month ==11 || month == 12)
correction = correction + 4;
return (month - 1) * 30 + correction + dayOfMonth;
} |
|
|
|
|
|
|
|
|
The first thing to note about the function definition is that it looks like a void function, except for the fact that the heading begins with the data type int instead of the word void. The second thing to observe is the return statement at the end, which includes an integer expression between the word return and the semicolon. |
|
|
|
|
|
|
|
|
A value-returning function returns one value, not through a parameter but by means of a return statement. The data type at the beginning of the heading declares the type of value that the function will return. This data type is called the function type, although a more proper term is function value type (or function return type or function result type). |
|
|
|
|
|