×

VB.Net Programs

VB.Net program to return an array of integers from a user-defined function

By Nidhi Last Updated : November 16, 2024

Return an array of integers from VB.Net function

Here, we will create a user-defined function that will return the array of integers to the calling function.

Program/Source Code:

The source code to return an array of integers from the user-defined function is given below. The given program is compiled and executed successfully.

VB.Net code to demonstrate the example of returning an array of integers from the function

'VB.Net program to return an array of integers 
'from a user-defined function.

Module Module1
    Function GetArray() As Integer()
        Dim arr() As Integer = New Integer(3) {1, 2, 3, 4}
        Return arr
    End Function

    Sub Main()
        Dim RetArr() As Integer

        RetArr = GetArray()

        Console.WriteLine("Elements of array:")
        For i = 0 To RetArr.Length - 1 Step 1
            Console.Write("{0} ", RetArr(i))
        Next
        Console.WriteLine()
    End Sub
End Module

Output

Elements of array:
1 2 3 4
Press any key to continue . . .

Explanation

In the above program, we created a module Module1 that contains a user-defined function GetArray() and Main() subroutine.

The Main() subroutine is the entry point for the program. Here we created a reference for integer array.

Here we called function GetArray() that returns an array of integers, and then printed the elements of the array on the console screen.

In the GetArray() function, we created an array arr and return the array from the function.

VB.Net User-defined Functions Programs »


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.