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.

76 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

Private Sub Add(num1 As Single, num2 As Single)<br />

‘Display numbers <strong>and</strong> their sum picResult.Print “The sum of”; num1; “<strong>and</strong>”;<br />

num2; “is”; num1 + num2<br />

End Sub<br />

Private Sub ExplainPurpose()<br />

‘Explain the task performed by the program<br />

picResult.Print “This program displays sentences”<br />

picResult.Print “identifying pairs of numbers <strong>and</strong> their sums.”<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 />

This program displays sentences<br />

identifying pairs of numbers <strong>and</strong> their sums.<br />

The sum of 2 <strong>and</strong> 3 is 5<br />

The sum of 4 <strong>and</strong> 6 is 10<br />

The sum of 7 <strong>and</strong> 8 is 15<br />

The variables num1 <strong>and</strong> num2 appearing in the Sub procedure Add are called parameters.<br />

They are merely temporary place holders for the numbers passed to the Sub procedure;<br />

their names are not important. The only essentials are their type, quantity, <strong>and</strong> order. In this<br />

Add Sub procedure, the parameters must be numeric variables <strong>and</strong> there must be two of<br />

them. For instance, the Sub procedure could have been written<br />

Private Sub Add(this As Single, that As Single)<br />

‘Display numbers <strong>and</strong> their sum<br />

picResult.Print “The sum of”; this; “<strong>and</strong>”; that; “is”; this + that<br />

End Sub<br />

A string also can be passed to a Sub procedure. In this case, the receiving parameter in<br />

the Sub procedure must be followed by the declaration As String.<br />

EXAMPLE 4<br />

The following program passes a string <strong>and</strong> two numbers to a Sub procedure. When the Sub procedure is<br />

first called, the string parameter state is assigned the string constant “Hawaii”, <strong>and</strong> the numeric parameters<br />

pop <strong>and</strong> area are assigned the numeric constants 1184000 <strong>and</strong> 6471, respectively. The Sub procedure<br />

then uses these parameters to carry out the task of calculating the population density of Hawaii. The second<br />

Call statement assigns different values to the parameters.<br />

Private Sub cmdDisplay_Click()<br />

‘Calculate the population densities of states<br />

picDensity.Cls<br />

Call CalculateDensity(“Hawaii”, 1184000, 6471)<br />

Call CalculateDensity(“Alaska”, 607000, 591000)<br />

End Sub<br />

Object Property Setting<br />

frmStates Caption State Demographics<br />

cmdDisplay Caption Display Demographics<br />

picDensity<br />

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

Dim rawDensity As Single, density As Single

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

Saved successfully!

Ooh no, something went wrong!