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.

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

[Run <strong>and</strong> click the comm<strong>and</strong> button.]<br />

■ PASSING ARRAYS BETWEEN PROCEDURES<br />

An array that is not dimensioned in the (Declarations) section of (General) but rather is declared<br />

in a procedure is local to that procedure <strong>and</strong> unknown in all other procedures. However,<br />

an entire local array can be passed to another procedure. The name of the array, followed by<br />

an empty set of parentheses, must appear as an argument in the calling statement, <strong>and</strong> an array<br />

variable name of the same type must appear as a corresponding parameter in the procedure definition<br />

of the procedure that is to receive the array.<br />

EXAMPLE 4<br />

The following program illustrates passing an array to both a Sub procedure <strong>and</strong> a Function procedure.<br />

Private Sub cmdDisplayAverage_Click()<br />

‘Pass array to Sub procedure <strong>and</strong> Function procedure<br />

Dim score(1 To 10) As Integer<br />

Call FillArray(score())<br />

picAverage.Cls<br />

picAverage.Print “The average score is”; Sum(score()) / 10<br />

End Sub<br />

Private Sub FillArray(s() As Integer)<br />

‘Fill array with scores<br />

s(1) = 85<br />

s(2) = 92<br />

s(3) = 75<br />

s(4) = 68<br />

s(5) = 84<br />

s(6) = 86<br />

s(7) = 94<br />

s(8) = 74<br />

s(9) = 79<br />

s(10) = 88<br />

End Sub]<br />

Private Function Sum(s() As Integer) As Integer<br />

Dim total As Integer, index As Integer<br />

‘Add up scores<br />

total = 0<br />

For index = 1 To 10<br />

total = total + s(index)<br />

Next index<br />

Sum = total<br />

End Function<br />

[Run <strong>and</strong> click the comm<strong>and</strong> button.]

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

Saved successfully!

Ooh no, something went wrong!