// Bob Myers // // Example illustrating some function declarations, definitions, calls // // This layout is okay, but not as desirable #include using namespace std; int Sum(int x, int y, int z) // add the three parameters together and return the result { int answer; answer = x + y + z; return answer; } double Average (double a, double b, double c) // add the parameters, divide by 3, and return the result { return (a + b + c) / 3.0; } bool InOrder(int x, int y, int z) // answers yes/no to the question "are these parameters in order, // smallest to largest?" Returns true for yes, false for no. // (This kind of function often known as a "predicate function") { if (x <= y && y <= z) return true; else return false; } /* functions have been DEFINED above. This satisfies declare-before-use * for anything appearing below. The function definitions also count as * declarations, because each function's header is the same as its * declaration */ int main() { int i1, i2, i3; double d1, d2, d3; cout << "Input three integers: "; cin >> i1 >> i2 >> i3; cout << "Input three doubles: "; cin >> d1 >> d2 >> d3; // samples of function calls double avg; int total; total = Sum(i1, i2, i3); avg = Average(d1, d2, d3); cout << "The sum of the three integers = " << total << '\n'; cout << "The average of the three doubles = " << avg << '\n'; // note that we can place the call in the cout statements, which // prints the return values cout << "The sum of 10, 14, and 18 = " << Sum(10, 14, 18) << '\n'; cout << "The average of 1.3, 2.7, and 6.9 = " << Average(1.3, 2.7, 6.9) << '\n'; // Notice that we can pass in integers where doubles are expected // legal via automatic-type-conversion rules cout << "The average of the three integers = " << Average(i1, i2, i3) << '\n'; // Testing out the boolean function (InOrder) if (InOrder(i1, i2, i3)) cout << "The three integers were typed in ascending order\n"; else cout << "The three integers were NOT typed in ascending order\n"; return 0; }