Visual Basic .NET Language Writing a function


Example

A function is a block of code that will be called several times during the execution. Instead of writing the same piece of code again and again, one can write this code inside a function and call that function whenever it is needed.

A function :

  • Must be declared in a class or a module
  • Returns a value (specified by the return type)
  • Has a modifier
  • Can take parameters to do its processing
Private Function AddNumbers(X As Integer, Y As Integer) As Integer
    Return X + Y
End Function

A Function Name, could be used as the return statement

Function sealBarTypeValidation() as Boolean
    Dim err As Boolean = False

    If rbSealBarType.SelectedValue = "" Then
        err = True
    End If

    Return err
End Function

is just the same as

Function sealBarTypeValidation() as Boolean
    sealBarTypeValidation = False        

    If rbSealBarType.SelectedValue = "" Then
        sealBarTypeValidation = True
    End If

End Function