VBA/Excel/Access/Word/Language Basics/Recursive

Материал из VB Эксперт
Перейти к: навигация, поиск

A recursive procedure is one that calls itself.

   <source lang="vb">

Public Function Factorial(N As Integer) As Integer

   If N <= 1 Then
       Factorial = 1
   Else
       Factorial = Factorial(N - 1) * N
   End If

End Function Sub res()

  Debug.Print Factorial(4)

End Sub

</source>
   
  


Recursive function to calculate the faculty of x

   <source lang="vb">

Public Sub testrecur()

 Debug.Print recur(3)

End Sub Function recur(x)

 If x <= 1 Then
   recur = 1
 Else
   recur = x * recur(x - 1)
 End If

End Function

</source>
   
  


Recursive Procedures

   <source lang="vb">

Function GetFactorial(intValue As Integer) As Double

   If intValue <= 1 Then
       GetFactorial = 1
   Else
       GetFactorial = GetFactorial(intValue - 1) * intValue
   End If

End Function Sub RecursiveFunction()

  Debug.Print GetFactorial(3)

End Sub

</source>