Recursion: 4 ways to get it working
#15
Nice patterns. Nice checkerboard. Amazing what can be done in a few lines of code.

I like this -- having the program show what's going on with recursion:

Code: (Select All)
_Title "Recursion Demo" ' dcromley
Screen _NewImage(1024, 768, 12)
Color 0, 15
Cls

Print Chr$(13) + "Finally, factorial(5)=" + Str$(factorial(5))

Function factorial (n)
  Dim t ' t exists for the duration of THIS level of recursion
  Print "Getting factorial(" + Str$(n) + ")"
  If n = 1 Then
    Print "factorial(1) = 1"
    factorial = 1
  Else
    Print "Need factorial(" + Str$(n - 1) + ")"
    t = factorial(n - 1)
    Print "Got factorial(" + Str$(n - 1) + ") =" + Str$(t)
    factorial = n * t
  End If
End Function
___________________________________________________________________________________
I am mostly grateful for the people who came before me.  Will the people after me be grateful for me?
Reply


Messages In This Thread
RE: Recursion: 4 ways to get it working - by dcromley - 02-17-2023, 08:58 PM



Users browsing this thread: 8 Guest(s)