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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

"Hawaii", 1184000, 6471<br />

"Alaska", 607000, 591000<br />

Private Sub cmdDisplay_Click()<br />

Dim state As String, pop As Single, area As Single<br />

Dim s As String, p As Single, a As Single<br />

'Calculate the population densities of states<br />

picDensity.Cls Open "DEMOGRAP.TXT" For Input As #1<br />

Input #1, state, pop, area<br />

Call CalculateDensity(state, pop, area)<br />

Input #1, s, p, a<br />

Call CalculateDensity(s, p, a)<br />

Close #1<br />

End Sub<br />

Private Sub CalculateDensity(state As String, pop As Single, area As Single)<br />

Dim rawDensity As Single, density As Single<br />

'The density (number of people per square mile)<br />

'will be rounded to a whole number<br />

rawDensity = pop / area density = Round(rawDensity)<br />

picDensity.Print "The density of "; state; " is "; density;<br />

picDensity.Print "people per square mile."<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 />

The density of Hawaii is 183 people per square mile.<br />

The density of Alaska is 1 people per square mile.<br />

FIGURE 3-2 Passing Arguments to Parameters in Example 6<br />

Arguments <strong>and</strong> parameters also can be used to pass values from Sub procedures back to<br />

event procedures or other Sub procedures. This important property is explored in detail in the<br />

next section.<br />

COMMENTS<br />

1. In this text, Sub procedure names begin with uppercase letters in order to distinguish<br />

them from variable names. Like variable names, however, they can be<br />

written with any combination of upper- <strong>and</strong> lowercase letters. To improve readability,<br />

the <strong>Visual</strong> <strong>Basic</strong> editor will automatically ensure that the capitalization<br />

of a Sub procedure name is consistent throughout a program. For instance, if<br />

you type Private Sub PROCEDURENAME <strong>and</strong> also type Call ProcedureName,<br />

the second name will be changed to match the first. Note: Parameters appearing<br />

in a Sub statement are not part of the Sub procedure name.<br />

Sub Procedures, Part I 79

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

Saved successfully!

Ooh no, something went wrong!