Another fact about programming that it's important to appreciate is that only
tiny programs will consist of a single procedure. Almost every program is
constructed by combining a number of interdependent procedures and functions.
Is that true of the procedure that we just wrote?
Click here for the answer}
Absolutely! The ``distance'' procedure can't stand on its own. It makes use
of the ``gravity'' and ``friction'' functions that we previously defined. We
have written a program that computes the distance covered by a block sliding on
a ramp, and this program consists of three building blocks: one procedure and
two functions.
In fact, programs often start off being quite small and then grow as more
functions and procedures are added. Let's see how this might work.
One thing that programmers like to do is to produce easy-to-read output. For
example, we might want to write a Mathematica procedure that would print out
informative labels for the results produced by the ``distance'' function.
Let's define a procedure called ``labPos'' that does just that.
labPos[t_, theta_]:=
Block[{},
Print["The ramp is elevated at ", theta, " radians"];
Print["After ", t, " seconds, the block has moved"];
Print[distance[t, theta], " meters"];
]
To see this procedure at work, run it. Here's a sample execution; you should
try others.
labPos[5, Pi/3]
Now let's look at the new procedure. This one consists of three statements,
each of which prints information to the display. The statements are executed
in order from first to last.
Notice that all kinds of information is printed out by this procedure,
including character strings, values of variables, a result returned by a
function, and a result returned by a procedure. See if you can find examples
of each of these in the procedure.
In what way does this procedure build upon previously existing building blocks?
Click here for the answer
Not surprisingly, it is possible to write C and Fortran code that looks very
similar to this.