Chapter 3: Functions
A function signature consists of a function name and the order and types of its parameters, but not its return type.
We use the term linkage to describe the visibility of names between program modules. When a function in one module is called from another module, for example, the function must have external linkage. By default, names of functions and variables are external. The opposite of external linkage is internal linkage, meaning that a name is visible only in its declared module.
Function name encoding is the process whereby the C++ compiler creates unique identifiers out of functions that have identical names but different parameter lists.
Preconditions are used when we want to state input requirements for function arguments. Postconditions explain what a function will do, given that the preconditions were satisfied. The assert macro can be used to enforce preconditions.
If a class object is passed to a function, it should be passed by reference to prevent the inefficient construction of a temporary object. Whenever possible, pass an object by constant reference to ensure that it cannot be modified.
A function with a non-constant reference parameter cannot receive a constant argument. But a function with a constant reference parameter can receive a non-constant argument.
Although it may be tempting to do so, never cast a constant object into a non-constant. The reason the object was declared const in the first place was to prevent it from being modified.
A function can be declared (with a function prototype) but not implemented, as long as the function is never called, and its address is never taken. Because of this, whenever you test a new class, be sure to call every member function to verify that the function was implemented.
Default function arguments are useful as a way to make functions more flexible, thus allowing them to be called with varying numbers of arguments. They can, for example, reduce the number of constructors in a class.
Never return a non-constant reference to a private class member; it would improperly allow a class client to have direct write access to the member.
A friend function is a global function that has been granted priveleged access to the private members of a specific class. Friend functions are often used to overload stream I/O operators.
Function overloading allows multiple functions with the same signature to exist in the same scope. Overloading is particularly useful for different functions that perform similar actions but have different parameter lists.
We discussed implicit conversions of function arguments. In general, when an argument of one type is passed to a function whose parameter list contains a similar type, the argument is converted to the parameter's type.
We demonstrated a way of preventing an input stream from entering an error state, by using an input string stream. This makes it much easier to recover from bad user input.
We demonstrated the stream manipulators setw, setfill, setprecision, oct, dec, and hex to control the formatting of numeric and string output.
We designed and implemented a Student Registration case study, demonstrating the specifications, analysis, design, and implementation steps. We plan to use this format for presenting case studies throughout the book.
We discussed recursion and showed an example of a recursive function that calculates the factorial of an integer. We also showed how a program can retrieve its command-line arguments.