Visual Basic .NET - Functions

Functions in VB.NET are a way to encapsulate code that can be reused in various parts of the application. Functions are named blocks of code that perform a specific task, and they can return a value to the calling code. The syntax of a function in VB.NET is as follows:

[Access Modifier] Function FunctionName ([Optional Parameters]) As ReturnType
    [Function Body]
    Return [Value]
End Function

The access modifier can be Public, Private, Protected, Friend, or Protected Friend, depending on the level of access you want to allow to the function. The Optional Parameters are parameters that can be omitted when the function is called, and the ReturnType is the data type that the function returns.

Here is an example of a simple function that takes two parameters and returns their sum:

Public Function AddNumbers(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
    Dim sum As Integer = num1 + num2
    Return sum
End Function

In this example, the function is called AddNumbers, and it takes two parameters, num1 and num2, both of type Integer. The function adds the two numbers together and stores the result in the sum variable, which is then returned to the calling code.

Functions can also have optional parameters, which are parameters that can be omitted when the function is called. Here is an example of a function that has an optional parameter:

Public Function MultiplyNumbers(ByVal num1 As Integer, Optional ByVal num2 As Integer = 1) As Integer
    Dim product As Integer = num1 * num2
    Return product
End Function

In this example, the function is called MultiplyNumbers, and it takes two parameters, num1 and num2. The second parameter, num2, is optional and has a default value of 1. This means that if the second parameter is omitted when the function is called, it will default to 1. The function multiplies the two numbers together and returns the product.

Functions can also be overloaded, which means that there can be multiple functions with the same name, but different parameter lists. Here is an example of an overloaded function:

Public Function AddNumbers(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
    Dim sum As Integer = num1 + num2
    Return sum
End Function
Public Function AddNumbers(ByVal num1 As Double, ByVal num2 As Double) As Double
    Dim sum As Double = num1 + num2
    Return sum
End Function

In this example, there are two functions with the same name, AddNumbers, but they take different parameter types. The first function takes two integers and returns an integer, while the second function takes two doubles and returns a double. The calling code can determine which function to call based on the parameter types.

FindMax - a function that takes an array of numbers and returns the maximum value.

Function FindMax(ByVal numbers() As Integer) As Integer
    Dim max As Integer = numbers(0)
    For i As Integer = 1 To numbers.Length - 1
        If numbers(i) > max Then
            max = numbers(i)
        End If
    Next
    Return max
End Function

IsPrime - a function that takes an integer and returns true if it is a prime number, false otherwise.

Function IsPrime(ByVal num As Integer) As Boolean
    If num <= 1 Then
        Return False
    End If
    For i As Integer = 2 To Math.Sqrt(num)
        If num Mod i = 0 Then
            Return False
        End If
    Next
    Return True
End Function

ConcatenateStrings - a function that takes an array of strings and returns a single string that is the concatenation of all the strings.

Function ConcatenateStrings(ByVal strings() As String) As String
    Dim result As String = ""
    For Each str As String In strings
        result &= str
    Next
    Return result
End Function

ReverseString - a function that takes a string and returns the reverse of the string.

Function ReverseString(ByVal str As String) As String
    Dim result As String = ""
    For i As Integer = str.Length - 1 To 0 Step -1
        result &= str(i)
    Next
    Return result
End Function

ConvertToBinary - a function that takes an integer and returns its binary representation as a string.

Function ConvertToBinary(ByVal num As Integer) As String
    Dim binary As String = ""
    While num > 0
        binary = (num Mod 2) & binary
        num \= 2
    End While
    Return binary
End Function