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.

222<br />

Passing Parameters<br />

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

expects an integer first and a string second, but this command gives it the<br />

data in the wrong order.<br />

If a subprogram doesn’t need any parameters, you can just call that subprogram<br />

by using its name, such as<br />

PrintMyName<br />

If you aren’t passing any parameters in some <strong>programming</strong> languages, you<br />

must leave the parameter list (the stuff between the parentheses) blank,<br />

such as<br />

printMyName ();<br />

Passing parameters by reference<br />

When a program calls and passes parameters to a subprogram, the computer<br />

makes duplicate copies of those parameters. One copy of those parameters<br />

stays with the main program and the second copy gets passed to the<br />

subprogram.<br />

Now if the subprogram changes those parameters, the values of those<br />

parameters stay trapped within the subprogram, as shown in Figure 6-6.<br />

Main program<br />

Subprogram<br />

Figure 6-6:<br />

Normally<br />

when you<br />

pass<br />

parameters<br />

to a<br />

subprogram,<br />

the<br />

computer<br />

makes a<br />

second<br />

copy <strong>for</strong> the<br />

subprogram<br />

to use.<br />

X = 9<br />

Y = 35<br />

ChangeMe (X, Y)<br />

Print ”X value = ”; X<br />

Print ”Y value = ”; Y<br />

This prints:<br />

X value = 9<br />

Y value = 35<br />

SUB ChangeMe (A as Integer, B as Integer)<br />

A = A + 7<br />

B = B + A<br />

Print ”A value = ”; A<br />

Print ”B value = ”; B<br />

END SUB<br />

This prints:<br />

A value = 16<br />

B value = 51<br />

When passing parameters by value, any changes the<br />

subprogram makes to the parameters won’t be sent back to<br />

any other part of the program.

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

Saved successfully!

Ooh no, something went wrong!