- Fundamental code building block
- Functions are the fundamental code building block of all programming
languages. All executable statements are in some function.
Functions provide a way to reuse code from many places in a
program, the effect of which is customized with parameters.
In addition, they form the fundamental conceptual
building blocks of the programmer.
- Local variables and parameters
-
Local variables are declared in the function body. They have no initial
value so you must give them a value before using them.
Their values are not
saved after the function returns, so they must be initialized each time.
Formal parameters are a kind of local variable that gets an initial value
from its corresponding actual parameter.
- Returning values, reference parameters, globals, and side-effects
- Functions exist to change things. There are four ways
they do this:
- return statement. If you produce one value,
this is the right way to return it (not in a global or reference parameter).
- Reference parameters. If you can't return a single value with
a return statement, you can often assign the value(s) to reference parameter(s).
- Global variables. You can assign to values to global variables.
You should avoid the use of global variables if at all possible, because
they are the source of many programming errors and make the program
less readable.
- System side-effects. A function can change things in the system or
user environment. The most common example of this is performing
I/O. It's a good idea to try to keep the I/O in a small number of
functions.
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.
- Parameter passing by value or reference
- In the function call, what is done with actual parameters depends on
the kind of formal parameter.
- value parameter.
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 type of the formal parameter in
the function header must be followed by &.
At the time of the call, the memory address
of the actual parameter is computed. This address is assigned
to the formal parameter.
Although you write value and reference parameters the same way inside a function body,
the C++ compiler knows the difference, and generates the appropriate
code for each.
This is a change from C, where the programmer has to whether to use an address
for an actual parameter and when using the formal parameter.
Java has thrown out the notion of reference parameters and explicit addresses,
and passes everything by value.
- 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.
- Misc. advanced issues
- Advanced topics not covered:
recursion, exceptions, function addresses,
variable length parameter lists, inline, register, static,
overloading, OOP considerations, ...
- 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.