Just for kicks, I took the final version of the statistics script we've been working with throughout this week, and “subroutinified” it. I broke the script up into its component parts, and put each of them into a subroutine. The actual body of the script, then, does nothing but call individual subroutines. There's no change in behavior to the script itself; just in how it's organized. Listing 11.1 shows the result:
Because this version of the stats script doesn't do anything functionally different from the one before it, there's only a couple of things to note here:
The only part of this script that doesn't live inside a subroutine definition are the lines 3 through 5, which call subroutines to initialize variables, to get the input from a file of numbers, and to calculate the results.
You may note that there are four subroutine definitions in this script, but only three subroutine calls at the top of the script. That's because the &printresults() subroutine calls the &printhist() subroutine at the end of its block.