Function Procedures

Function Procedures are procedures which procedures return a value. Visual Basic includes system-provided, or intrinsic functions, like Sqr, Cos or Chr. In addition, you can use the Function statement to write your own Function procedures.

The syntax for a Function procedure is:

[Private|Public][Static]Function procedurename (arguments) [As type]
statements
End Function

Like a Sub procedure, a Function procedure is a separate procedure that can take arguments, perform a series of statements, and change the value of its arguments. The arguments for a Function procedure work in exactly the same way as the arguments for a Sub procedure. Aside from the Function keyword, there are three differences between Sub and Function procedures:

Ø Generally, you call a function by including the function procedure name and arguments on the right side of a larger statement or expression.

Ø Function procedures have data types, just as variables do. This determines the type of the return value. (In the absence of an As clause, the type is the default Variant type.)

Ø You return a value by assigning it to the procedurename itself. When the Function procedure returns a value, this value can then become part of a larger expression. For example, you could write a function that calculates the third side, or hypotenuse, of a right triangle, given the values for the other two sides:

Function Hypotenuse (A, B)

Hypotenuse = Sqr(A ^ 2 + B ^ 2)

End Function

You call a Function procedure the same way you call any of the built-in functions in Visual Basic:

Label1.Caption = Hypotenuse(CInt(Text1.Text), CInt(Text2.Text))

X = Hypotenuse(Width, Height)

No comments:

Post a Comment