|
|
C++ tutorial: functions
Functions are the basic organizing unit in C programs
No practical C/C++ program is simple enough for its entire operation to be contained within
the 'main' function, as the previous examples have been. It is now time properly
to introduce the idea of functions: the basic unit of organization in C programs.
In C++, a higher-level unit of organization is introduced: the class. This
does not mean that functions are unimportant: quite the opposite is the case.
So what is a function? It is simply a self-contained piece of program with
certain inputs and outputs. When a function is activated it is said to be called.
Functions can call other functions, so in effect a complex job can broken into
progressively smaller, simpler ones. This principle is called stepwise
refinement.
If you have been following the example used so far, you may have noticed functions
being introduced without much comment. For example, in welcome2.cpp,
a trivial procedure was divided into two even more trivial functions. Before
starting a description of how to define more useful functions, we will have a
look at some of the built-in functions provided in the 'standard library'.
|