VB.NET Functions

This article covers the use of VB.NET Functions.

Definition

A function is a reusable block of code that runs only when called. Functions saves time, increase re-usability and makes your code simpler to read. Functions make use of arguments and parameters to receive data and output a result accordingly. This allows for a greater deal of flexibility on your code.

VB.NET offers both Functions as well as Sub Procedures. The difference between them is that Functions return a value, while Procedures do not. Returning values will be explained further in this article.


Defining a Function

VB.NET Functions follow the below format. Not all of the Keywords used down there are all compulsory.

Protection_Level Function Function_Name (Parameters) As Return_Type
     'Statements to be executed
End Function
  • Protection level can be set to either Public, Private or Protected. Changing the protection level changes what can access this function.
  • Function Name must be a unique name which will be used to call this function.
  • Parameters contain all the input parameters for this function. More on parameters and arguments below.
  • Return Type determines the data type of the returned value.

Arguments and Parameters – The difference

These two terms are very interchangeable with each other, so knowing the difference isn’t that big of a deal. The terms they refer to are almost identical.

Parameters are the variables present in the parenthesis during function definition. When a method is called, the arguments are the data you pass into the method’s parameters.

Function arguments

Arguments are used to pass information into the function from the rest of the program. The function can then be used to process this data and return a result.

There is no limit to the number of Arguments that can be written. There may even be no arguments at all, depending on the kind of function you’re making. Use commas to separate arguments in a function.

Keep in mind the number of arguments you insert into a function call. The number of arguments should be exactly equal to the number of parameters.


Function Example

In the example below, the variable name is the input parameter, where as the value, “Jim”, passed in the function call is the argument. Note the position of the Function. It is between the Module statement and Sub Main(). Remember that Main() is also a method (Function).

 Module Module1

    Function testfunction(ByVal name As String) As String
        Return "Hello " + name
    End Function

    Sub Main()
        Dim result As String

        result = testfunction("Jim")
        Console.WriteLine(result)
   
    End Sub
End Module
Hello Jim

All parameters have to be defined with their data types and with either ByVal or ByRef. ByVal sends a copy of the argument’s value to the procedure. ByRef sends a reference indicating where the value is stored, allowing the procedure to actually change the argument’s original value. Unless you know what you’re doing, stick to using ByVal.

The rest is self -explanatory. The function takes “Jim” as an input, concatenates it to “Hello” and prints it. This function can be run as many times as you wish simply by repeating the function call. Notice that Return does not print out the value on its own. It simply “returns” the value, what happens afterword is upto you.


Functions with Multiple Arguments

In this example we’ll go make a simple function with two parameters.

Module Module1
    Function Add(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
        Return num1 + num2
    End Function

    Sub Main()
        Dim result As Integer

        result = Add(5, 2)
        Console.WriteLine(result)

        result = Add(6, 3)
        Console.WriteLine(result)



    End Sub
End Module
7
9

This marks the end of the VB.NET Functions article. Any suggestions or contributions for CodersLegacy are more than welcome. Any questions can be asked below in the comment section.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments