Design of Functions
Functions are to do one thing, only one thing, and do it very well.
- One Thing and Only One Thing
For example, a function designed to find the average of two numbers
should return the average. It should NOT print the average. By
limiting
the function to a single purpose, the function becomes more general. You
may want to use this function in a program that doesn't require the
printing of the answer, or that requires it to be printed in a particular
format.
- And Do It Very Well
We could write the function Average() to take ints and return an int,
however, this will usually give an inaccurate answer, and we're limited
to using ints. A better way would be to have it take two doubles and
return a double. That way the returned average will be more accurate, and
the function will be more general. You can still pass the function
integers or even floats, since the values will be coerced into the types
of the formal parameters.