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.

Private Sub cmdDetermine_Click()<br />

Dim nom As String<br />

‘Determine a person’s first name<br />

nom = txtFullName.Text<br />

picFirstName.Cls<br />

picFirstName.Print “The first name is “; FirstName(nom)<br />

End Sub<br />

Private Function FirstName(nom As String) As String<br />

Dim firstSpace As Integer<br />

‘Extract the first name from a full name<br />

firstSpace = InStr(nom, “ “)<br />

FirstName = Left(nom, firstSpace - 1)<br />

End Function<br />

[Run, type Thomas Woodrow Wilson into the text box, <strong>and</strong> then click the comm<strong>and</strong> button.]<br />

The input to a user-defined function can consist of one or more values. Two examples of functions<br />

with several parameters follow. One-letter variable names have been used so the mathematical<br />

formulas will look familiar <strong>and</strong> be readable. Because the names are not descriptive,<br />

the meanings of these variables are carefully stated in comment statements.<br />

Private Function Hypotenuse(a As Single, b As Single) As Single<br />

‘Calculate the hypotenuse of a right triangle<br />

‘having sides of lengths a <strong>and</strong> b<br />

Hypotenuse = Sqr(a ^ 2 + b ^ 2)<br />

End Function<br />

Private Function FV(p As Single,r As Single,c As Single,n As Single)<br />

As Single Dim i As Single, m As Single<br />

‘Find the future value of a bank savings account<br />

‘p principal, the amount deposited<br />

‘r annual rate of interest<br />

‘c number of times interest is compounded per year<br />

‘n number of years<br />

‘i interest rate per period<br />

‘m total number of times interest is compounded<br />

i = r / c<br />

m = c * n<br />

FV = p * ((1 + i) ^ m)<br />

End Function<br />

EXAMPLE 3<br />

The following program uses the Hypotenuse function.<br />

Function Procedures 89

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

Saved successfully!

Ooh no, something went wrong!