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.

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

FIGURE 3-1 Passing Arguments to Parameters<br />

This feature allows values obtained as input from the user to be passed to a Sub procedure.<br />

EXAMPLE 5<br />

The following variation of the addition program requests the two numbers as input from the user. Notice<br />

that the names of the arguments, x <strong>and</strong> y, are different from the names of the parameters. The names of<br />

the arguments <strong>and</strong> parameters may be the same or different; what matters is that the order, number, <strong>and</strong><br />

types of the arguments <strong>and</strong> parameters match.<br />

Private Sub cmdCompute_Click()<br />

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

‘This program requests two numbers <strong>and</strong><br />

‘displays the two numbers <strong>and</strong> their sum.<br />

x = Val(txtFirstNum.Text)<br />

y = Val(txtSecondNum.Text)<br />

Call Add(x,, y)<br />

End Sub<br />

Object Property Setting<br />

frmAdd Caption Add Two Numbers<br />

lblFirstNum Caption First Number<br />

txtFirstNum Text (blank)<br />

lblSecondNum Caption Second Number<br />

txtSecondNum Text (blank)<br />

cmdCompute Caption Compute Sum<br />

picResult<br />

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

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

picResult.Cls<br />

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

End Sub<br />

[Run, type 23 <strong>and</strong> 67 into the text boxes, <strong>and</strong> then click the comm<strong>and</strong> button.]<br />

EXAMPLE 6<br />

The following variation of Example 4 obtains its input from the file DEMOGRAP.TXT. The second Call<br />

statement uses different variable names for the arguments to show that using the same argument names is<br />

not necessary. See Figure 3-2.<br />

DEMOGRAP.TXT contains the following two lines:

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

Saved successfully!

Ooh no, something went wrong!