Next: Input/Output
Up: C Primer CS 1005
Previous: switch
In most non-trivial programs, functions need to be declared before
their implementation is given (``prototypes''). Normally, this is done
in the ``header''-file (ending on .h) belonging to a
C-file. Variable names can be omitted in the prototype (and types
in the implementation) if that does not lead to confusion.
maximum.h
// Prototype:
int max(int , int );
maximum.cxx
#include "maximum.h"
// Implementation:
int max (int a, int b) {
int max;
if (a>b) max = a;
else max = b;
return max;
}
- Parameter types can be references (marked by
&) 3.
- Passing (and returning results) by value (int max(int a,
int b)) makes a copy of the data (i.e., in the example, a
and b would be copied at the beginning of the function, then
the local variable max is copied again to provide a return
value from the function).
- Passing by reference (int max(int &a, int &b)) does not
copy data but may change the original (passed) data in the calling
function.
- Marking reference parameters constant
(
Item max(const Item &a, const Item &b)
) makes data in the
calling function non-changeable.
Andreas Koeller
2000-06-04