Kardi Teknomo
Kardi Teknomo Kardi Teknomo Kardi Teknomo
   
 
  Research
  Publications
  Tutorials
  Resume
  Personal
  Contact

 

How to Make a Function in VB?

By Kardi Teknomo, PhD.

<VB Tutorial | Contents | Previous | Next >

As example of function procedure, now you will make a simple function to compute hypotenuse of a right triangle that is the longest side of triangle facing the perpendicular angle.

Start making a new standard Exe project. Put one label and two text boxes in the form and one command button and change the Text property of the 2 textboxes into any numbers. Then copy and paste the function code below in the declaration section of your form or standard module.

 Function Hypotenuse (A As Integer, B As Integer) As String
       Hypotenuse = Sqr(A ^ 2 + B ^ 2)
 End Function
             
Private Sub Command1_Click()
       Label1.Caption = “Hypotenuse = “ & Hypotenuse(CInt(Text1.Text), CInt(Text2.Text))
End Sub 

Run the program, fill the two text box with numbers and click the command button. In the example above the function Hypotenuse take two integer arguments A and B and then return the computation value (by repeating the name of the function) to the caller. In this case the caller is Command1_Click event procedure. Then, the caller put the return value into label

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

<VB Tutorial | Contents | Previous | Next >

Rate this tutorial or give your comments about this tutorial

This tutorial is copyrighted.

Preferable reference for this tutorial is

Teknomo, Kardi. Visual Basic Tutorial . http:\\people.revoledu.com\kardi\ tutorial\VB

 

 

 

 
© 2007 Kardi Teknomo. All Rights Reserved.
Designed by CNV Media