Chapter 4.9

Traps and Tips

AmazonA function is the building block of the C++ language. It allows you to compose your program in an organized way making the program easier to read and maintain.

Using functions effectively will truly improve your programming skills and therefore make the development time finite, hence Finite Programming in C++, the title of the book. To this end I’ve collected a list of tips and traps you need to be aware of related to C++ function.

You may declare a function prototype, member or nonmember, without ever defining it. There will no problem if you don’t use that function. If you do use it, however, then you need to define it.

Use inline instead of #define to define short functions.

Use const instead of #define to define constants.

Whenever calling a function, think about whether you want the arguments passed by reference or by value.

Function preconditions and postconditions should always be preserved. When you complete a function think about whether it can deal with every possible value as its arguments without crashing. Think about what it should do when it receives incorrect arguments.

Out of our five fingers, the fingernail of our middle finger grows the most quickly.