- 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:
- return statement. If a function produces one value,
it should
return
it (not in a global or reference parameter).
- 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).
- 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.
- 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.
- value parameter.
Value parameters are the default.
At the time of the call, the actual parameter is evaluated to a value,
and this value is assigned to the corresponding formal parameter.
- reference parameter.
The function header indicates a reference parameter by
following the parameter type by &.
At the time of the call, the memory address, not value,
of the actual parameter is assigned
to the formal parameter. All references to the formal parameter
are to the memory of the actual parameter. Reference parameters
are used for two main purposes:
(1) to allow the function to change the value, and (2) to refer to large structures
that would be costly to pass as an actual parameter.
The corresponding actual parameter must be something that can be
assigned to (is an l-value).
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
-
- Make function and parameter names describe what they do/are.
- Indent the code within the body of a function.
- Use the parameter names in function prototypes.
- Functions should generally not be longer than one printed page.
- 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.