15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

214<br />

Creating and Using Subprograms<br />

Figure 6-1:<br />

Every large<br />

program is<br />

made up of<br />

smaller<br />

subprograms<br />

that<br />

act as<br />

building<br />

blocks.<br />

A program can consist of one<br />

massive chunk of code.<br />

Subprograms divide a large program<br />

into smaller pieces to make the<br />

large program easier to create and<br />

modify.<br />

If you wanted to write a horse race prediction program, you wouldn’t have<br />

to write the whole thing from scratch. You could copy the subprograms from<br />

another program and reuse them in your new project, as shown in Figure 6-2.<br />

By reusing subprograms, you can create more complicated programs faster<br />

than be<strong>for</strong>e. After programmers create enough useful subprograms, they can<br />

store these subprograms in a “library” that they and other programmers can<br />

use in the future.<br />

Creating and Using Subprograms<br />

A subprogram essentially yanks out two or more commands from your main<br />

program and stores them in another part of your main program or in a separate<br />

file, as shown in Figure 6-3.<br />

The reasons <strong>for</strong> isolating commands in a subprogram (and out of your main<br />

program) are to<br />

✦ Keep your main program smaller and thus easier to read and modify.<br />

✦ Isolate related commands in a subprogram that can be reused.<br />

✦ Make <strong>programming</strong> simpler and faster by just reusing subprograms<br />

from other projects.

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

Saved successfully!

Ooh no, something went wrong!