C++ Notes: Functions

Fundamental code building block
Functions are the fundamental code building block of all programming languages. All executable statements must be in a function. Functions provide a way to reuse code which is customized with parameters. Along with classes they form the fundamental conceptual building blocks.
Local variables and parameters
Local variables (auto) are declared in a function body. They are allocated memory on the call stack when the function is entered are deallocated when the function returns. They have no fixed initial value so they must be given a value before use. A formal parameter is a kind of local variable that gets its initial value from the corresponding actual parameter.
Returning values, reference parameters, globals, and side-effects
Functions change things. There are four ways they do this:
  1. return statement. If a function produces one value, it should return it (not in a global or reference parameter).
  2. Reference parameters. If a function produces more than one result, the return statement can't easily be used. The standard solution is to pass some or all of the values back thru reference parameter(s).
  3. Global variables. You can assign to values to global variables, but avoid globals if at all possible, because they are a frequent source of programming errors.
  4. System side-effects. A function can change things in the system or user environment. The most common example of this is I/O.
Global variables and side-effects make functions very difficult to reuse in other programs.
Function prototypes at beginning of program
C/C++ require functions to be declared (not necessary defined) before using them. It is common to put function prototypes at the beginning of the code or in an include file. The standard style for declaring classes uses this style.
Parameter passing by value or reference
In the function call, what is done with actual parameters (also called arguments) depends on the nature of the corresponding formal parameter. In C before C++ there were no reference parameters. This same effect was achieved by explicitly passing an address and explicitly dereferencing the formal parameter in the function.
Converting actual parameters types to match formal parameter types
There is a somewhat complicated set of rules for this, but you can generally assume it is done the same way as for assignment. It's a good style to write explicit casts so that readers of the program realize what is happening, and it's no less efficient.
Style issues
Other topics
Not covered: recursion, exceptions, function addresses, variable length parameter lists, inline, register, static, overloading, overriding, template functions, ...
Vocabulary
Be familiar with: function, subroutine, procedure, subprogram, prototype, function declaration, function definition, function header, void, call, return, parameter, actual parameter, argument, formal parameter, parameter list, local variable, global variable, side-effects, value parameter, reference parameter and use of &, passing by value, passing by reference, stack, stack frame.