VB - Functions


In this tutorial I'd like to explain 'Functions' in visual basic; what they are and what they are used for.

Functions are tipically used to make your codes more clear.
They are also used to do complicated calculations.

For this tutorial I'll teach you how to make a simple function.

1.Start by opening or creating a new project and add a button to your form.

Double click the button and add the following codes:

Dim Intnumber1, intnumber2 As Integer
Intnumber1 = Integer.Parse(InputBox("Number 1 please?"))
intnumber2 = Integer.Parse(InputBox("Number 2 please?"))

To create a function, you will need to declare the function first;

You type in:

Function NameOfTheFunction(Byval variable as integer, ByVal variable2 as integer) as Integer

End Function


Go back to the codes of button1 and add the following codes:


Go back to the function you created and add the following codes:

'Return' simply means it will return the value it obtained by adding number1 to number2 in the variable intResult.

So when you run your form and click the button it will ask you for 2 numbers. The function will then create a new variable, intResult, and it will make a sum of the 2 numbers you entered and then it will show you the result in a messagebox.

Download the source codes:
Function.rar (60,1 kB)

Topic: VB - Functions

No comments found.