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

Possibly Related Threads…
Thread Author Replies Views Last Post
  plotting shapes not working madscijr 6 658 10-22-2025, 05:29 AM
Last Post: SMcNeill
  why isn't _FULLSCREEN working? madscijr 5 533 09-20-2025, 01:47 AM
Last Post: SMcNeill
  I'm working on an old MS-DOS thingy... KlamPs 2 519 08-25-2025, 09:43 AM
Last Post: hsiangch_ong
  blue circle isn't drawing and print isn't working? madscijr 12 2,351 09-21-2024, 06:13 PM
Last Post: madscijr
  Updating Clock at a different rate to program loop-example not working as expected? dowster 1 618 08-15-2024, 12:06 PM
Last Post: luke

Forum Jump:


Users browsing this thread: 1 Guest(s)