C++ Notes: Function Concepts

Purpose of functions

The purpose of functions is to make the programmer's life easier.

Naming

For a function to work well as a conceptual unit it has to have a name that is clear to the human reader.

Cohesion

Each function should do only one thing.

Side-effects

Functions should avoid side-effects such as communicating by means of global variables.

Functional Decomposition - top-down programming

The top-down programming style is to write a short main program that calls on additional functions to accomplish it task. These functions should similarly be kept short and call more functions if necessary to get their task done. Start with the main function, and write only function stubs which do almost nothing except maybe print a message that they were called. Get this running, then expand one of the stubs, get this running, ... . This very productive style of programming is related to iterative programming and is one of the cornerstones of Extreme Programming.

One page

It is generally a good idea to keep functions shorter than one page or one screen. This makes them easy to comprehend, If one page is not enuf space, then write additional functions to help perform the task (top-down programming).