15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

36<br />

Planning Ahead with Structured Programming<br />

One of these smaller tasks might be, “Identify the lottery numbers that tend to<br />

appear often.” A second task might be, “Pick the six numbers that have appeared<br />

most often and display those as the potential future winning numbers.”<br />

The idea is that writing a large program may be tough, but writing a small program<br />

is easy. So if you keep dividing the tasks of your program into smaller<br />

and smaller parts, eventually you can write a small, simple program that can<br />

solve that task. Then you can paste these small programs together like building<br />

blocks, and you’ll have a well-organized big program — theoretically.<br />

Now if you need to modify part of the large program, just find the small program<br />

that needs changing, modify it, and plug it back into the larger program,<br />

and you’ve just updated the larger program.<br />

Ideally, each small program should be small enough to fit on a single sheet of<br />

paper. This makes each small program easy to read, understand, and modify.<br />

When you divide a large program into smaller programs, each small program<br />

is a subprogram.<br />

If you divide a program into multiple subprograms, you have two options <strong>for</strong><br />

where to store your subprograms:<br />

✦ Store all of your subprograms in a single file.<br />

This option is fine <strong>for</strong> small programs, but after you start dividing a program<br />

into multiple subprograms, trying to cram all of your subprograms<br />

into a single file is like trying to cram your entire wardrobe of shirts, pants,<br />

and underwear into your sock drawer. It’s possible, but it makes finding<br />

anything later that much more difficult.<br />

✦ Store subprograms in separate files, as shown in Figure 2-4.<br />

Main Program<br />

Main Program<br />

Subprogram #1<br />

Figure 2-4:<br />

You can<br />

store<br />

subprograms<br />

in one big<br />

file or in<br />

separate<br />

files.<br />

Subprogram #2<br />

Subprogram #3<br />

Subprogram #4<br />

A single file can hold the<br />

main program plus all of<br />

its subprograms.<br />

Subprogram #1<br />

Subprogram #2<br />

Subprogram #3<br />

Subprogram #4<br />

You can store the main<br />

program and all of its<br />

subprograms in separate<br />

files.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!