UMBC CS 201, Fall 05
 UMBC CMSC 201 Fall '05 CSEE | 201 | 201 F'05 | lectures | news | help Search Notes:

# 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
```
int Average (int integer1, int integer2) ;
```
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.
```
double Average (double number1, double number2) ;
```
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 automatically coerced into the types of the formal parameters.

CSEE | 201 | 201 F'05 | lectures | news | help

Tuesday, 20-Sep-2005 14:04:13 EDT