Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Exiting sub while inside a loop PhilOfPerth 5 525 12-05-2025, 09:40 AM
Last Post: PhilOfPerth
  Do Loop, Sleep and Mouse Button Dimster 5 599 09-06-2025, 12:57 PM
Last Post: Dimster
  Using modulo to loop through lists fistfullofnails 3 724 09-03-2025, 11:50 PM
Last Post: fistfullofnails
  What is wrong with this for/next loop Helium5793 6 1,152 04-15-2025, 05:11 PM
Last Post: Kernelpanic
  Question on ln in a for/next loop Dimster 13 2,251 09-13-2024, 11:07 PM
Last Post: Kernelpanic

Forum Jump:


Users browsing this thread: 1 Guest(s)