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.

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

‘will be displayed rounded to a whole number<br />

rawDensity = pop / area<br />

density = Round(rawDensity) ‘round to whole number<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 />

The parameters in the density program can have any names, as with the parameters in<br />

the addition program of Example 3. The only restriction is that the first parameter be a string<br />

variable <strong>and</strong> that the last two parameters be numeric variables. For instance, the Sub procedure<br />

could have been written<br />

Private Sub CalculateDensity(x As String, y As Single, z 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 = y / z<br />

density = Round(rawDensity)<br />

picDensity.Print “The density of “; x; “ is”; density;<br />

picDensity.Print “people per square mile.”<br />

End Sub<br />

When nondescriptive names are used for parameters, the Sub procedure should contain<br />

comments giving the meanings of the variables. Possible comments for the preceding program<br />

are<br />

‘x name of the state’<br />

y population of the state’<br />

z area of the state<br />

■ VARIABLES AND EXPRESSIONS AS ARGUMENTS<br />

The items appearing in the parentheses of a Call statement are called arguments. These should<br />

not be confused with parameters, which appear in the heading of a Sub procedure. In Example<br />

3, the arguments of the Call Add statements were constants. These arguments also could have<br />

been variables or expressions. For instance, the event procedure could have been written as follows.<br />

See Figure 3-1.<br />

Private Sub cmdAdd_Click()<br />

Dim x As Single, y As Single, z As Single<br />

‘Display the sum of two numbers<br />

picResult.Cls<br />

Call ExplainPurpose<br />

picResult.Print<br />

x = 2<br />

y = 3<br />

Call Add(x, y)<br />

Call Add(x + 2, 2 * y) z = 7<br />

Call Add(z, z + 1)<br />

End Sub<br />

Sub Procedures, Part I 77

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

Saved successfully!

Ooh no, something went wrong!