02-10-2023, 06:34 PM
(02-09-2023, 05:28 PM)Dimster Wrote: Ah.. so in a Do Loop the counter doesn't need to be Dimensioned or Shared but in a Recursive call it does.
You don't need a counter. A recursive function calls itself until: n - n = 0. Applies: 0! = 1. This ends the recursive call, and the value is popped off the stack and displayed. (That's how I understand it.)
Recursive calculation using the factorial as an example.
Code: (Select All)
'Fakultaet rekursiv - 10. Feb. 2023
$Console:Only
Option _Explicit
Declare Function Fakultaet(n As Integer) As _Integer64
Dim As Integer n
Locate 2, 3
Print "Rekursive Berechnung der Fakultaet - (n!)"
Locate 4, 3
Input "Fakultaet von (n): ", n
Locate 5, 3
Print Using "Die Fakultaet von ### ist: ###,###,###"; n, Fakultaet(n)
End 'Hauptprogramm
Function Fakultaet (n As Integer)
Dim As _Integer64 fakul
If n = 0 Or n = 1 Then
fakul = 1
Else
fakul = Fakultaet(n - 1) * n
End If
Fakultaet = fakul
End Function