Why does my Loop end after 11 Loops?
#46
(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

[Image: Rekursion-Beispiel.jpg]
Reply


Messages In This Thread
Why does my Loop end after 11 Loops? - by Dimster - 02-06-2023, 07:08 PM
RE: Why does my Loop end after 11 Loops? - by Kernelpanic - 02-10-2023, 06:34 PM



Users browsing this thread: 18 Guest(s)