02-11-2023, 05:47 PM
(This post was last modified: 02-11-2023, 05:50 PM by Kernelpanic.)
I only took the example from C because it was explained well there. It can also be found in the QuickBasic 4 manual under 2.44.
I don't know whether your code example really runs recursively, I will have to try it out. To me it looks like it is rather iterative. Looks like a procedure to me too.
This is now the factorial iterative. It doesn't need a stack. In the past, in the days of Win 95, you could still see a time difference between iterative and recursive, especially when calculating the Fibonacci number. With normal calculations one can today no longer see the difference.
I don't know whether your code example really runs recursively, I will have to try it out. To me it looks like it is rather iterative. Looks like a procedure to me too.
This is now the factorial iterative. It doesn't need a stack. In the past, in the days of Win 95, you could still see a time difference between iterative and recursive, especially when calculating the Fibonacci number. With normal calculations one can today no longer see the difference.
Code: (Select All)
'Fakultaet iterativ mit FOR-Schleife - 11. Feb. 2023
$Console:Only
Option _Explicit
Declare Function Fakultaet(n As Integer) As _Integer64
Dim As Integer n
Locate 2, 3
Print "Iterative 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) Static
Dim As _Integer64 fakul
Dim As Integer i
fakul = 1
For i = 1 To n
fakul = fakul * i
Next
Fakultaet = fakul
End Function