01-29-2023, 04:33 PM
Here is the example you provided for Recursion by Subroutine
And here is your Recursion by Function
I actually hadn't thought that Recursion by Label was a thing.
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.