19.04.2017 Views

Learn to Program with Small Basic

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

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

Subroutines are basic building blocks for creating large programs (see<br />

Figure 10-1). In this chapter, you’ll delve in<strong>to</strong> the wild world of subroutines,<br />

learn how <strong>to</strong> move data in and out of them, and use them <strong>to</strong> build large<br />

programs and fun games!<br />

Figure 10-1: Subroutines are the<br />

building blocks of larger programs<br />

Why Use Subroutines?<br />

Let’s say you run a construction company. Your job is <strong>to</strong> coordinate the work<br />

among your contrac<strong>to</strong>rs and build houses. As a manager, you don’t have <strong>to</strong><br />

know all the nitty-gritty details of building a home: the plumber handles the<br />

plumbing, the roofer shingles the roof, and the electrician runs all the wires.<br />

Each contrac<strong>to</strong>r knows their job and is always ready <strong>to</strong> work when they<br />

receive your call.<br />

That’s very similar <strong>to</strong> how subroutines<br />

work! Each subroutine has its own name, like<br />

how the plumber’s name is Mario. Each subroutine<br />

does something different, just like how<br />

the plumber and the roofer have different jobs,<br />

but all are needed <strong>to</strong> build the house. As the<br />

programmer, you’re the manager, and your job<br />

is <strong>to</strong> solve problems as you build your program.<br />

You call your contrac<strong>to</strong>rs (that is, your subroutines)<br />

and let them know when you need them<br />

<strong>to</strong> work (see Figure 10-2). You start writing the<br />

program by typing statements in the edi<strong>to</strong>r. When you need <strong>to</strong> perform a<br />

job that a subroutine handles, you just call that subroutine and wait. When<br />

the subroutine completes its task, you move on <strong>to</strong> the next step in your<br />

program.<br />

There’s nothing new about this call-and-wait strategy; you’ve been<br />

doing it since Chapter 1. When you call an object’s method, you’re actually<br />

giving the work <strong>to</strong> that object in the <strong>Small</strong> <strong>Basic</strong> library. Subroutines are<br />

like methods, but you have <strong>to</strong> write all the statements in the subroutines.<br />

Subroutines help you organize your thinking process and make it easier<br />

<strong>to</strong> fix errors.<br />

Boss<br />

Hello, Bob.<br />

Please work<br />

on the roof.<br />

Bob<br />

Sure, boss.<br />

I’ll let you know<br />

when I’m done.<br />

Figure 10-2: The boss (main<br />

program) calling the Bob<br />

subroutine<br />

Writing Subroutines<br />

Let’s use a fun example <strong>to</strong> learn how <strong>to</strong> write a subroutine: in his travels,<br />

Gulliver had dinner <strong>with</strong> the king and queen of Lilliput (the land of tiny<br />

people). During dinner, the king explained that he was 8.5 glum-gluffs tall.<br />

130 Chapter 10

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

Saved successfully!

Ooh no, something went wrong!