×

VB.Net Programs

VB.Net program to demonstrate the recursive function

By Nidhi Last Updated : November 13, 2024

Recursive function in VB.Net

Here, we will create a recursive function Recursive() that will print numbers from 5 to 1 on the console screen.

Program/Source Code:

The source code to demonstrate the recursive function is given below. The given program is compiled and executed successfully.

VB.Net code to demonstrate the example of recursive function

'VB.Net program to demonstrate the recursive function.

Module Module1

    Function Recursive(ByVal count As Integer) As Integer
        Console.Write("{0} ", count)
        count = count - 1

        If (count = 0) Then
            Return count
        End If

        Return Recursive(count)
    End Function

    Sub Main()
        Recursive(5)
        Console.WriteLine()
    End Sub
    
End Module

Output

5 4 3 2 1
Press any key to continue . . .

Explanation

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

The Main() subroutine is the entry point for the program. Here, we call the function Recursive() with value 5.

In the Recursive() function, we printed the value of parameter count and decrease the value of variable count by 1 till it becomes 0. In every recursive call value of the count get a decrease and print an updated value on the console screen.

VB.Net User-defined Functions Programs »


Comments and Discussions!

Load comments ↻





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