19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

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

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

dure by just using the variable in the Sub procedure? How do Sub procedures pass values back<br />

to an event procedure? The answers to these questions provide a deeper underst<strong>and</strong>ing of the<br />

workings of Sub procedures <strong>and</strong> reveal their full capabilities.<br />

■ PASSING VALUES BACK FROM SUB PROCEDURES<br />

Suppose a variable, call it arg, appears as an argument in a Call statement, <strong>and</strong> its corresponding<br />

parameter in the Sub statement is par. After the Sub procedure is executed, arg will<br />

have whatever value par had in the Sub procedure. Hence, not only is the value of arg passed<br />

to par, but the value of par is passed back to arg.<br />

EXAMPLE 1<br />

The following program illustrates the transfer of the value of a parameter to its calling argument.<br />

Private Sub cmdDisplay_Click()<br />

Dim amt As Single<br />

‘Illustrate effect of value of parameter on value of argument<br />

picResults.Cls<br />

amt = 2<br />

picResults.Print amt;<br />

Call Triple(amt)<br />

picResults.Print amt<br />

End Sub<br />

Private Sub<br />

Triple(num As Single)<br />

‘Triple a number picResults.Print num;<br />

num = 3 * num<br />

picResults.Print num;<br />

End Sub<br />

[Run <strong>and</strong> then click the comm<strong>and</strong> button. The following is displayed in the picture box.]<br />

2 2 6 6<br />

Although this feature may be surprising at first glance, it provides a vehicle for passing<br />

values from a Sub procedure back to the place from which the Sub procedure was called. Different<br />

names may be used for an argument <strong>and</strong> its corresponding parameter, but only one<br />

memory location is involved. Initially, the cmdDisplay_Click( ) event procedure allocates a<br />

memory location to hold the value of amt (Figure 3-4(a)). When the Sub procedure is called,<br />

the parameter num becomes the Sub procedure’s name for this memory location<br />

(Figure 3-4(b)). When the value of num is tripled, the value in the memory location becomes<br />

6 (Figure 3-4(c)). After the completion of the procedure, the parameter name num is forgotten;<br />

however, its value lives on in amt (Figure 3-4(d)). The variable amt is said to be passed<br />

by reference.<br />

FIGURE 3-4 Passing a Variable by Reference to a Sub Procedure<br />

Passing by reference has a wide variety of uses. In the next example, it is used as a vehicle<br />

to transport a value from a Sub procedure back to an event procedure.<br />

Sub Procedures, Part II 81

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

Saved successfully!

Ooh no, something went wrong!