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.

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

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

End Function<br />

The value of each of the preceding functions is assigned by a statement of the form<br />

FunctionName = expression. The variables t <strong>and</strong> nom appearing in the preceding functions<br />

are parameters. They can be replaced with any variable of the same type without affecting the<br />

function definition. For instance, the function FtoC could have been defined as<br />

Private Function FtoC(temp As Single) As Single<br />

‘Convert Fahrenheit temperature to Celsius<br />

FtoC = (5 / 9) * (temp - 32)<br />

End Function<br />

Like Sub procedures, Function procedures can be created from a code window with Alt/T/P.<br />

The only difference is that the circle next to the word Function should be selected. After the<br />

name is typed <strong>and</strong> the OK button is clicked, the lines Private Function FunctionName( ) <strong>and</strong><br />

End Function will be placed automatically (separated by a blank line) in the code window.<br />

EXAMPLE 1<br />

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

Private Sub cmdConvert_Click()<br />

picTempC.Cls<br />

picTempC.Print FtoC(Val(txtTempF.Text))<br />

End Sub<br />

Private Function FtoC(t As Single) As Single<br />

‘Convert Fahrenheit temperature to Celsius<br />

FtoC = (5 / 9) * (t - 32)<br />

End Function<br />

Object Property Setting<br />

frm4_3_1 Caption Convert Fahrenheit to<br />

Celsius<br />

lblTempF Caption Temperature<br />

(Fahrenheit)<br />

txtTempF Text (blank)<br />

cmdConvert Caption Convert to Celsius<br />

lblTempC Caption Temperature (Celsius)<br />

picTempC<br />

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

EXAMPLE 2<br />

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

Object Property Setting<br />

frm4_3_2 Caption Extract First Name<br />

lblName Caption Name<br />

txtFullName Text (blank)<br />

cmdDetermine Caption Determine First Name<br />

picFirstName

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

Saved successfully!

Ooh no, something went wrong!