Suppose for a moment we have just finished performing several experiments and each of these experiments has produced a set of data points. Suppose we are required to find the average value for each individual experiment.
> experiment_1 := {33, 234, 5233, 122, 32843, 9}:
> experiment_2 := {6435, 2234, 22, 8902, 30183}:
> experiment_3 := {8893, 902, 10283, 9918020, 52, 2356}:
.
.
.
We could accomplish this easily using our knowledge of the for
loop and the length function.
> total := 0:
> for i from 1 to length(experiment_1) do
> total := total + experiment_1[i]:
> od:
> lprint('The average is ', total/length(experiment_1));
The average is 6412.3333
Unfortunately, it becomes an exhausting typing exercise to repeat
this code for each experiment, each time replacing experiment_i
with experiment_i+1. It would be nice to encapsulate this short
sequence of statements into a single construct which would perform the
same repetitive action but on different sets. The procedure
construct in Darwin does exactly this.
Procedures, when properly used, enable programmers to build on the work of previous programmers, they make programs easier to read by hiding the low-level details of an operation, and they make modifying your programs in the future less painful.