What is a Function?

 < Day Day Up > 



A function is a collection of logically related program statements written to perform a specific processing activity. A function is also a code module. A function is given a name and with this name the function can be called or executed by any program that needs to use the function. The program statements that comprise the body of the function give the function its behavior. Function behavior can be built upon the behavior of other functions. In other words, functions can call other functions. Once a function is written and its behavior defined, you can effectively forget about the details of how the function performs its duties and call the function in the program when needed.

Grouping often-repeated program statements into a function saves memory space, but, most importantly, allows you to break apart a complex processing problem into a set of process abstractions. Just like well-chosen variable names lend a level of abstraction to data, well-chosen function names provide a way to achieve process abstraction.

A well-written or well-formed function can be used in many different programs on many different computers by many different programmers. This functionally is achieved by giving the function a singular purpose, or, in other words, maximizing its cohesiveness while at the same time decoupling it as much as possible from other program elements. The rule of thumb: maximize cohesion – minimize coupling.

A function can simply be a collection of often-repeated statements that returns no value. Functions of this type are extremely useful in their own right and are what Pascal programmers would call procedures. On the other hand, functions can communicate the results of their processing via either return values or parameter references.

Interface vs. Implementation

All you need to know about a function to use it is its interface. A function’s interface describes certain characteristics of the function such as its name, what type, if any, it returns, and what parameters, if any, it needs to perform its job. When writing a function, you will first declare the function’s interface and then define what it means to be that function. You declare a function’s interface by declaring a function prototype. You define what a function does by adding the necessary code to the body of a function definition to achieve the function’s purpose.

Put Function Interface Declarations in Header Files

Place function declarations in header files. This is a good habit to form early in your programming career because you will do the exact same thing with class declarations. If you’re writing a small program with only a few functions you can group all the required function declarations into one header file. If you’re writing a large application, you can logically group related function declarations together. Putting function declarations in header files is the key to writing function libraries.

#ifndef...#define....#endif

Your header file should use the #ifndef, #define, & #endif preprocessor directives to allow you to include it in any file that needs access to the function declarations it contains. The structure of a typical header file will look like this:

#ifndef HEADER_NAME_H #define HEADER_NAME_H // function declarations appear here #endif

Substitute your own header name where HEADER_NAME_H appears above. I use the name of the header file, in all caps, separating words and extensions with underscores. For example, if I name a header file myheader.h then the #ifndef, #define, #endif structure would look like so:

#ifndef MY_HEADER_H #define MY_HEADER_H // function declarations appear here #endif

The file myheader.h can now be included in other source files using the #include preprocessor directive without fear of getting multiple declaration errors:

#include "myheader.h"

Put Function Definitions in Implementation Files

Place function definitions in a separate implementation file. An implementation file in C++ has the extension .cp or .cpp. Whereas the header file contains the function’s interface declaration, the implementation file will contain the definition of the function. A function definition gives meat or meaning to a function. It is where you as a programmer define exactly what it is the function will do using C++ programming statements in the body of the function. Everything you have learned about C++ up to this chapter, and a whole lot more, can be used when you write the function code.

Characteristics of a Well-Written Function

A well-written function should exhibit several fundamental characteristics. Several of these were briefly discussed above. A function should serve one purpose and its purpose should be reflected in its name, so it should be named well too. A function that does what it should and nothing surprising is said to be highly cohesive. In function writing you should strive to maximize the cohesiveness of your functions.

A function, to the fullest extent possible, should stand on its own, and not be too tightly coupled to other program elements. This characteristic, referred to as coupling, should, to the fullest extent possible, be minimized. The danger of having tightly coupled functions or code modules is that a change to one function may affect the behavior of another function or code module somewhere else in your program. Perhaps the most difficult task you face as a programmer of not just functions, but of object-oriented programs in general, is the minimization of intermodule dependencies or intermodule coupling.

Table 9-1 summarizes the characteristics of a well-written function.

Table 9-1: Characteristics of Well-Written Functions

Characteristic

Description

Singular purpose/Maximally cohesive

The program statements in the body of the function are logically related and exist to implement function behavior as described by the function’s interface declaration (prototype). There should be nothing surprising going on in the body of the function that isn’t hinted at in the function name.

Well-named

A function’s name should reflect the function’s purpose. Since most functions perform an action, function names should be formed from action words (verbs).

Minimally coupled

Take steps to reduce a function’s dependency on other program elements. You can do this by using local function variables when possible, and passing other required program elements to a function via arguments.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net