Using a Function

Now that we have seen how to write the Average function, let's see how to use it to solve our original problem. The program in Figure 5.5 uses our Average function twice, once to average two weights and once to average two ages.

Figure 5.5. Using the Average function (code\func1.cpp)
 #include <iostream> using namespace std; short Average(short First, short Second) { short Result; Result = (First + Second) / 2; return Result; } int main() { short FirstWeight; short SecondWeight; short FirstAge; short SecondAge; short AverageWeight; short AverageAge; cout << "Please type in the first weight: "; cin >> FirstWeight; cout << "Please type in the second weight: "; cin >> SecondWeight; AverageWeight = Average(FirstWeight, SecondWeight); ...

Get C++: A Dialog Programming with the C++ Standard Library now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.