Loops alternate recursive ways
#6
Here is the example you provided for Recursion by Subroutine

Code: (Select All)
'_Title "Recursion example" 'b+ 2021-10-27
_Title "Recursion example by Subroutine"

'Recursion is a sub or function that calls itself until it's job is done
'Here is a grahics
Const pi = _Pi
Screen _NewImage(700, 700, 32)

recThis 700 / 2, 700 / 2, 350 / 2, 1

Sub recThis (x, y, arm, level As Integer)
    'first thing to ask in recursive subroutine is are we done!!!
    If arm < 2 Then Exit Sub ' recursion is finished
    ' no not done
    If level Mod 2 Then
        x1 = x + arm * Cos(0): y1 = y + arm * Sin(0)
        x2 = x + arm * Cos(pi): y2 = y + arm * Sin(pi)
        Line (x1, y1)-(x2, y2)
    Else
        x1 = x + arm * Cos(pi / 2): y1 = y + arm * Sin(pi / 2)
        x2 = x + arm * Cos(1.5 * pi): y2 = y + arm * Sin(1.5 * pi)
        Line (x1, y1)-(x2, y2)
    End If
    recThis x1, y1, arm * .7, level + 1
    recThis x2, y2, arm * .7, level + 1
End Sub

And here is your Recursion by Function

Code: (Select All)
'_Title "Recursion example" 'b+ 2021-10-27
_Title "Recursion example by Function"

'Recursion is a sub or function that calls itself until it;s job is done
'Here is a text example
Const pi = _Pi
Screen _NewImage(700, 700, 32)
s$ = "This is an example of bplus using recursion to reverse this text."
Print s$
Print reverseMeSomeMore$(s$, 0)

Function reverseMeSomeMore$ (text$, level)
    ' are we there yet?
    If level = Int(Len(text$) / 2) + 1 Then reverseMeSomeMore$ = text$: Exit Function
    s$ = Mid$(text$, level, 1)
    c$ = Mid$(text$, Len(text$) - level + 1, 1)
    Mid$(text$, level, 1) = c$
    Mid$(text$, Len(text$) - level + 1, 1) = s$
    reverseMeSomeMore$ = reverseMeSomeMore$(text$, level + 1)
End Function

I actually hadn't thought that Recursion by Label was a thing.
Reply


Messages In This Thread
Loops alternate recursive ways - by bplus - 01-28-2023, 07:45 PM
RE: Loops alternate recursive ways - by Dimster - 01-28-2023, 09:09 PM
RE: Loops alternate recursive ways - by bplus - 01-28-2023, 09:28 PM
RE: Loops alternate recursive ways - by Dimster - 01-29-2023, 04:10 PM
RE: Loops alternate recursive ways - by bplus - 01-29-2023, 04:23 PM
RE: Loops alternate recursive ways - by Dimster - 01-29-2023, 04:33 PM
RE: Loops alternate recursive ways - by bplus - 01-29-2023, 05:45 PM
RE: Loops alternate recursive ways - by Dimster - 01-30-2023, 05:36 PM
RE: Loops alternate recursive ways - by bplus - 01-30-2023, 07:11 PM
RE: Loops alternate recursive ways - by SMcNeill - 02-01-2023, 06:10 AM
RE: Loops alternate recursive ways - by Dimster - 02-01-2023, 02:05 PM



Users browsing this thread: 1 Guest(s)