Following the flow of
execution is one way to read programs, but as you saw in the previous
section, it can quickly become labyrinthine. An alternative is what I
call the “leap of faith.” When you come to a function call, instead of
following the flow of execution, you assume that the function works
correctly and returns the appropriate value.
In fact, you are already
practicing this leap of faith when you use built-in functions. When you
call Cos or Exp, you don’t examine the implementations of those
functions. You just assume that they work, because the people who wrote
the built-in libraries were good programmers.
Well, the same is true
when you call one of your own functions. For example, we wrote a
function called IsSingleDigit that determines whether a number is
between 0 and 9. Once we have convinced ourselves that this function is
correct – by testing and examination of the code – we can use the
function without ever looking at the code again.
One major risk is that the “trusted” function may become inadvertently
corrupted by it’s interaction with other program elements. There is a
concept called encapsulation, which addresses itself to minimizing this
risk. It will be discussed briefly in later sections, but a full
discussion is beyond the scope of this book. However, if you are
planning on writing large general C++ programs, you should become very
familiar with this concept.