02-14-2023, 01:27 PM
(This post was last modified: 02-14-2023, 02:26 PM by TempodiBasic.)
Hi
I think that this demo is clear enough to be used as example about recursion in QB64pe.
I must remark that the STATIC way has is goal in preserving the previouse values of variable of the SUB/FUNCTION.
So if we need to preserve few variables we can use STATIC into the SUB to declare the variable to preserve,
instead if we need to preserve all variable or the more part of local variables we use STATIC in SUB/FUNCTION declaration.
more explanation and tips coming soon.
I think that this demo is clear enough to be used as example about recursion in QB64pe.
I must remark that the STATIC way has is goal in preserving the previouse values of variable of the SUB/FUNCTION.
So if we need to preserve few variables we can use STATIC into the SUB to declare the variable to preserve,
instead if we need to preserve all variable or the more part of local variables we use STATIC in SUB/FUNCTION declaration.
Code: (Select All)
Rem Demonstration of variables into recursive calling
Screen 0
Dim counter As Single
Dim Shared counter2 As Single
Dim Choice As String
Choice = " "
Do
If Choice <> "" Then
Cls
Print "we are testing recursive calling"
Print String$(60, "#")
Print "please make your choice: "
Print " press 1 for recursion without parameter or shared variable"
Print " press 2 for recursion with parameter and no shared variable"
Print " press 3 for recursion with shared variable and no parameter"
Print " press 4 for STATIC recursion without parameter or shared variable"
Print " press 0 to exit from demonstration"
Print String$(60, "#")
End If
Choice = InKey$
If Choice = "0" GoTo Ending
If Choice = "1" Then GoSub NoParameters
If Choice = "2" Then GoSub YesParameters
If Choice = "3" Then GoSub SharedVariable
If Choice = "4" Then GoSub StaticNoParameters
Loop
End
NoParameters:
counter = 0
Print " No parameter and no shared variable demo"
Print "-----------------------------------------"
Print counter; " value of flag in the main"
RecursiveNoParameters
Return
YesParameters:
counter = 0
Print " Yes parameter and no shared variable demo"
Print "------------------------------------------"
Print counter; " value of flag in the main"
RecursiveYesParameters counter
Return
SharedVariable:
counter2 = 0
Print " No parameter and Yes shared variable demo"
Print "------------------------------------------"
Print counter2; " value of flag in the main"
SharedVariables
Return
StaticNoParameters:
counter = 0
Print " STATIC and no parameter and no shared variable demo"
Print "-----------------------------------------"
Print counter; " value of flag in the main"
StaticNoParameter
Return
Ending:
Rem here the flow of code ends
End
Sub RecursiveNoParameters
counter = counter + 1
DoJob counter
If InKey$ <> "" Then Exit Sub ' emergency exit
If counter < 10 Then RecursiveNoParameters
End Sub
Sub RecursiveYesParameters (c As Single)
c = c + 1
DoJob c
If InKey$ <> "" Then Exit Sub ' emergency exit
If c < 10 Then RecursiveYesParameters c
End Sub
Sub SharedVariables
counter2 = counter2 + 1
DoJob counter2
If InKey$ <> "" Then Exit Sub ' emergency exit
If counter2 < 10 Then SharedVariables
End Sub
Sub StaticNoParameter
Static counter ' you need to have STATIC only the flag of recursion, at least
counter = counter + 1
DoJob counter
If InKey$ <> "" Then Exit Sub ' emergency exit
If counter < 10 Then StaticNoParameter
End Sub
Sub DoJob (c As Single)
Print c; " press a key to stop the recursive loop"
Sleep 1 ' we need this to avoid the crash of application
End Sub
more explanation and tips coming soon.