Functions in visual basic

FUNCTIONS IN VISUAL BASIC

Functions and subs are almost the same the only different is that a FUNCTION returns a value while a SUB does not.



Consider the example below.

Private Function one() As String

        Dim answer As String

        answer = 0

        For i = 1 To 15

            answer = answer & vbNewLine & i

        Next

        Return answer

End Function

This function places numbers 0 to 15 into a variable called answer.

The syntax

Private Function [name of your function]() As [data type of what is to be returned]

        Code to be executed

        Return [what should be returned]

    End Function

 

You start by declaring your function.

Private Function [name of your function]() As data type of what is to be returned]

The term private means that the function can only be used by that program only. We can also declare a function as a Public Function

The name of the function should be descriptive of what the function does for ease of knowing its functions.

Note after the name of the function we have this bracket ()

Then we declare the data type that should be returned by the function.

Code to be executed

Then we write down the actual code to be executed.

Return [what should be returned]

This is what the function should return.

End Function

This marks the end of the function.

Let us now look at the example above and what it does.

 Private Function one() As String

We have declared a private function called one and it is returning data of type string.

Dim answer As String

We are declaring a variable of type string for us to work with in the code

answer = 0

we are then saying that the initial value of the variable is 0.

For i = 1 To 15

We are now creating a loop starting from 1 to 15

answer = answer & vbNewLine & i

we are telling vb whatever is in the variable answer should be retained,

That is

answer = answer

to add a new line that is

vbNewLine

then add whatever is is in the variable i.

Next

We are telling vb to increase the value of i and repeat the loop again.

This goes on until the value of i is equal to 15.

Return answer

Whatever is in the variable answer is what is now returned.

End Function

We then end the function.

 



LET US DO IT PRACTICALLY

Design a new project and give it a suitable name maybe function.

Once the form loads and a button onto it and leave its text property as just button1.

FUNCTION1

Your form should now look like the one below.

Double click on the button1 add the following code after public class and before the private sub button1_click

Public Class Form1

Private Function one() As String

        Dim answer As String

        answer = 0

        For i = 1 To 15

            answer = answer & vbNewLine & i

        Next

        Return answer

    End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

End Sub

End Class

 

CALLING A FUNCTION.

Our function so far is doing nothing and it has to be called in order to perform a task. You can even try to test the program and of course nothing will happen.

This is the way to call a function  just write its name as shown below.

Function name()

Eg in our example above.

one()

so let go back to our project and call our function.

Once the project is loaded add a textbox to the form. Check the multi line property of the textbox on the properties window and change it to true from false. Then resize to as shown below.

 

FUNCTION2

Double click on the button now and enter this code highlighted in green

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

TextBox1.Text = one()

End Sub

We telling the function and whatever should be returned should be displayed in textbox1.

Your code window should now look like the one below.

FUNCTION3

 

Test your project now. Once the form is displayed click on the button.

This should be your results

FUNCTION4