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.

132<br />

Dividing a Program into Subprograms<br />

Dividing a Program into Subprograms<br />

The smaller the program, the easier it is to write, read, and modify later. So<br />

rather than create a massive program, with related commands organized into<br />

groups, you can divide a large program into smaller pieces, or subprograms.<br />

Subprograms essentially break up a large program into multiple miniature<br />

programs with each miniature program acting like a building block to create<br />

a much larger program, as shown in Figure 1-4. So rather than build a large<br />

program entirely out of keywords, you can build a program out of keywords<br />

and subprograms (which are themselves made up of keywords).<br />

Math subprogram<br />

Math commands<br />

Math commands<br />

Figure 1-4:<br />

Subprograms<br />

create<br />

reusable<br />

building<br />

blocks that<br />

you can use<br />

to make<br />

writing<br />

programs<br />

even easier.<br />

Saving subprogram<br />

Printing subprogram<br />

Subprograms act like bigger<br />

building blocks that make a<br />

large program easier to read<br />

and understand.<br />

Saving commands<br />

Saving commands<br />

Printing commands<br />

Printing commands<br />

Printing commands<br />

The program on the left,<br />

divided into subprograms, is<br />

equivalent to this program<br />

with every command visible.<br />

Think of subprograms as a way to create keywords that aren’t built into the<br />

<strong>programming</strong> language. For example, the C <strong>programming</strong> language doesn’t<br />

have any keywords <strong>for</strong> working with text strings, so programmers have used<br />

C’s existing keywords to create subprograms that can work with text strings.<br />

By using these existing subprograms, other C programmers can manipulate<br />

text strings without writing their own commands.<br />

Using keywords alone to create a program is like trying to build a skyscraper<br />

out of bricks. It’s possible, but it takes a long time to layer enough bricks to<br />

reach the height of a typical 50-story skyscraper.

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

Saved successfully!

Ooh no, something went wrong!