C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Count: The count represents the total number of times the Function has been called, no matter the calling pattern.
Output: Let's examine the output of the program. We see that the Recursive function is called six times.
And: After 5 is multiplied by 2 five times, we have the result 160. On the sixth function call, the multiplication does not occur.
VB.NET program that uses recursive method
Module Module1
Function Recursive(ByVal value As Integer, ByRef count As Integer) As Integer
Console.WriteLine("Recursive({0}, {1})", value, count)
count = count + 1
If value >= 100 Then
Return value
End If
Return Recursive(value * 2, count)
End Function
Sub Main()
Dim count As Integer = 0
Dim total As Integer = Recursive(5, count)
Console.WriteLine("Total = {0}", total)
Console.WriteLine("Count = {0}", count)
End Sub
End Module
Output
Recursive(5, 0)
Recursive(10, 1)
Recursive(20, 2)
Recursive(40, 3)
Recursive(80, 4)
Recursive(160, 5)
Total = 160
Count = 6
Tip: You could always just use a shared variable. This is inelegant but really just as good in most program contexts.
And: In critical programs, iteration is typically more reliable and easy to debug, and thus preferred.