Private Function Fac(ByVal Num As Decimal) As Decimal
'Factorial
If Num = 1 Then
Return 1
Else
Return Num * Fac(Num - 1)
End If
End Function
This function uses tail-end recusion, that is, the last thing it does is recur. It uses:
Return Num * Fac(Num - 1)
instead of
Return Fac(Num - 1) * Num
Because both would give you the same answer, but I know that some languages (Scheme, for instance) optimize tail-end recursion. So, if you can help it, recurring is the last thing you should do, and you code will fun faster.
Does anyone know if VB does this?




Reply With Quote