Chapter 6

Breaking a Large Program into Subprograms

IN THIS CHAPTER

Bullet Using a subprogram

Bullet Passing parameters

Bullet Using recursion

The bigger the program, the harder that program is to read, fix, and modify. Just as it’s easier to spot a spelling mistake in a recipe printed on a single page than it is to find that same spelling mistake buried inside a 350-page cookbook, it’s easier to fix problems in a small program than it is in a big one.

Because small programs can perform only simple tasks, the idea behind programming is to write a lot of little programs and paste them together, like building blocks, creating one massive program. Because each little program is part of a much bigger program, those little programs are subprograms, as shown in Figure 6-1.

Warning The biggest problem with dividing a large program into multiple subprograms is to make each subprogram as independent, or loosely coupled, as possible. That means if one subprogram fails, it doesn’t wreck the entire program along with it, like yanking out a single playing card from a house of cards.

FIGURE 6-1: Every large program is made ...

Get Beginning Programming All-in-One For Dummies, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.