Summary

 < Day Day Up > 



Functions provide a convenient way to package program behavior into manageable units with an eye towards reuse. A function is a collection of logically related program statements written to perform a specific processing activity. A function is also a code module; it is given a name, and with its name, it 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.

Every function needs to be declared and defined. A function declaration or prototype is a statement of a function’s interface. Put function prototypes in separate header files and function definitions in their own implementation files. This separates a function’s interface from its implementation and makes it easier to create function libraries. Prevent multiple header file inclusion by using the preprocessor directives #ifndef, #define, and #endif.

A well-written function has the following characteristics: It is maximally cohesive, it is well-named, and it is minimally coupled.

It is helpful to think of a function as a world unto itself or as a “black box”. Variables declared within the body of a function are called local variables. Static function variables exist across function calls, are initialized during the first call to the function in which they appear, and retain their value between function calls. Automatic local variables are initialized during every function call. Local function variables mask global variables with the same name. Function parameters have local scope.

Arguments can be passed to functions by value or by reference. The advantage to passing arguments to functions by reference is realized when using large arguments such as user-defined data structures.

Functions can return values. Where possible have only one return statement in a function. If you must have more than one return statement, keep them close together to aid clarity. Do not return the address of a local variable. This is a common programming error that results in a dangling reference.

Functions can be overloaded. An overloaded function shares a name but differs in parameter type and number. Overloaded functions are said to have different function signatures. The compiler resolves which version of an overloaded function to call based on the number and type of arguments used to call the function.

Recursive functions are written to solve special types of problems. A recursive function calls itself and must eventually come to a halt or else it will recurse forever.

Function pointers have many interesting uses, one of them being to implement callback functions. An understanding of function pointers leads to a better understanding of the C++ virtual function calling mechanism.

When you have come up with a great function, or several great functions, convert them into a library to make their reuse easier

Whew! You learned a lot in this chapter. Great! You will use every bit of it as you progress through the text. Study hard!



 < 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