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.

Passing Parameters 221<br />

Figure 6-5:<br />

When you<br />

call a<br />

subprogram,<br />

you may<br />

also need<br />

to pass<br />

parameters<br />

to that<br />

subprogram.<br />

Main program<br />

PrintMyName (5, ”John Smith”)<br />

When you call a subprogram,<br />

you can pass it parameters . . .<br />

5<br />

. . . which get stored in the parameter list<br />

defined by the subprogram.<br />

”John Smith”<br />

SUB PrintMyName (PrintTimes as Integer, Name as String)<br />

FOR I = 1 TO PrintTimes<br />

PRINT Name<br />

NEXT I<br />

END SUB<br />

Subprogram<br />

Book II<br />

Chapter 6<br />

When you call a subprogram, you must give it the exact number and type of<br />

parameters it expects to receive. So the PrintMyName subprogram accepts<br />

two parameters whereas the first parameter must be an integer and the<br />

second parameter must be a string, such as<br />

PrintMyName (4, “Hal Berton”)<br />

PrintMyName (53, “Billie Buttons”)<br />

Breaking a Large<br />

Program into<br />

Subprograms<br />

If you don’t give a subprogram the right number of parameters, your program<br />

doesn’t work. So if a subprogram is expecting two parameters, the following<br />

doesn’t work because they don’t give the subprogram exactly two<br />

parameters:<br />

PrintMyName (4)<br />

PrintMyName (4, 90, “Roberta Clarence”)<br />

The first command doesn’t work because the PrintMyName subprogram<br />

expects two parameters, but this command passes only one parameter.<br />

The second command doesn’t work because this command passes<br />

three parameters, but the PrintMyName subprogram expects only two<br />

parameters.<br />

Another problem is that you give the subprogram the exact number of<br />

parameters, but not the right type of parameters. So this subprogram<br />

expects to receive an integer and a string, so the following subprogram calls<br />

don’t work because they give it the wrong data:<br />

PrintMyName (98, 23)<br />

PrintMyName (“Victor Harris”, 7)<br />

The first command doesn’t work because the PrintMyName subprogram<br />

expects an integer and a string, but this command tries to give it two numbers.

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

Saved successfully!

Ooh no, something went wrong!