Simple recursion example 2
In simple general terms a recursion routine can be an efficient way to do a sort of looping while looking for something. Sometimes the idea of "tunnelling down" or digging down" can describe the situation well. Sometimes a standard looping routine of the Do Loop While type form can replace a recursion routine more efficiently ( https://www.excelforum.com/tips-and-...omparison.html )
The characteristic that possibly distinguishes recursion routines is the ability to seemingly intelligently go up and down: Compare it to the situation of digging down , whereby from time to time you come back up a bit, then dig down again in a slightly different direction. That is best illustrated by using a recursion routine for one of its most common practical uses, that of searching through Folders and sub Folders in a Directory. That will be done in the over next post.
As a pre requisite to that we will demonstrate how a much simpler recursion routine may dig down until a condition is met, and then, thereafter it comes back up, level for level , i.e. copy for copy. This usage is very similar to the standard looping routine of the Do Loop While type form except that you have a last coming back up the levels, or coming back up the copies, which you would not have with a simple Do Loop While type form ( https://www.excelforum.com/developme...ml#post4221234 )
We did not experience this coming back up in the last code because we Stoped. In general use of a recursion process, we do not have a simple way to end with like If ____ Then End Sub. Such a solution would be difficult to implement in a recursion process, as we would be ending the current copy with the End Sub.
So generally a recursion process ends by somehow "coming back up levels" or ending each copy one after the other, either
in sequence for a simple routine,
or
after going back and forth / up and down in a more complicated implementation of a recursion process.
In most cases the coming back up is rarely experienced. We have specifically a message box at that point to show when a copy of the routine is ended. In practical uses what happens at this point is ether nothing, or for more complex implementation of recursion, we may be in a Loop at that point which would determine if we "go back down" again: Some controlled looping at this point is what triggers the possibility to "go back down" , pseudo…
Sub Sub2( CpyNo , ______ )
CurrentCopyNumber=CpyNo
'
'
__Do
___Call Sub2(CurrentCopyNumber+1 , ______ )
__Loop While__
MsgBox Prompt:="You are Ending Sub2 , copy " & CurrentCopyNumber
End Sub
This will be discussed in the over next post.
For now we look at the simple case
One immediate way to stop us going further than say the second copy, would be to change our last coding pair from…_
Code:
Sub Sub1()
Dim StrtCpyNo As Long ' To count copy number of code instructions being run
Let StrtCpyNo = 1
Call Sub2(CpyNo:=StrtCpyNo, Msg:="Initial Message")
MsgBox Prompt:="Ending main procedure"
End Sub
' Code above is Main calling procedure '____________________________________________________________________
' Code below is called procedure
Sub Sub2(ByVal CpyNo As Long, ByVal Msg As String)
Dim CopyNo As Long ' This is to indicate which copy of Sub2 is currently running
Let CopyNo = CpyNo
MsgBox Prompt:="You are Starting Sub2 , copy " & CopyNo
Call Sub2(CpyNo:=CopyNo + 1, Msg:="Message coming from Sub2, copy " & CopyNo & "")
MsgBox Prompt:="You are Ending Sub2 , copy " & CopyNo
End Sub
_... to
Code:
Sub Sub1()
Dim StrtCpyNo As Long ' To count copy number of code instructions being run
Let StrtCpyNo = 1
Call Sub2(CpyNo:=StrtCpyNo, Msg:="Initial Message")
MsgBox Prompt:="Ending main procedure"
End Sub
' Code above is Main calling procedure '_____________________________________________________________________________
' Code below is called procedure
Sub Sub2(ByVal CpyNo As Long, ByVal Msg As String)
Dim CopyNo As Long ' This is to indicate which copy of Sub2 is currently running
Let CopyNo = CpyNo
MsgBox Prompt:="You are Starting Sub2 , copy " & CopyNo
If CopyNo < 2 Then Call Sub2(CpyNo:=CopyNo + 1, Msg:="Message coming from Sub2, copy " & CopyNo & "")
MsgBox Prompt:="You are Ending Sub2 , copy " & CopyNo
End Sub
You can safely run the above coding, ( by running Sub1 ) , in normal mode, as it will no longer try to go on for ever. But it is probably more demonstrative to use debug F8 mode
Here is an attempt to show the last run as Excel VBA actually experienced it, - running Sub1 followed by two separate copies of Sub2, or rather
Start Sub1
_Start Sub2Copy1
___Start Sub2Copy2
___End Sub2Copy2
_End Sub2Copy1
End Sub1
Code:
Sub Sub1Sub2Sub2()
Dim StrtCpyNo As Long ' To count copy number of code instructions being run
Let StrtCpyNo = 1
'Call Sub2(CpyNo:=StrtCpyNo, Msg:="Initial Message")
' Sub2 Copy 1
Dim CpyNo As Long: Let CpyNo = StrtCpyNo: Dim Msg As String: Let Msg = "Initial Message"
Dim CopyNo As Long ' This is to indicate which copy of Sub2 is currently running
Let CopyNo = CpyNo
MsgBox Prompt:="You are Starting Sub2 , copy " & CopyNo
If CopyNo < 2 Then
'Call Sub2(CpyNo:=CopyNo+1, Msg:="Message coming from Sub2, copy " & CopyNo & "")
' Sub2 Copy 2
Dim CpyNo_ As Long: Let CpyNo_ = CopyNo + 1: Dim Msg_ As String: Let Msg_ = "Message coming from Sub2, copy " & CopyNo & ""
Dim CopyNo_ As Long ' This is to indicate which copy of Sub2 is currently running
Let CopyNo_ = CpyNo_
MsgBox Prompt:="You are Starting Sub2 , copy " & CopyNo_
End If
If CopyNo_ < 2 Then
Else
MsgBox Prompt:="You are Ending Sub2 , copy " & CopyNo_
End If
'End Sub2 ' End Copy 2 of Sub2
MsgBox Prompt:="You are Ending Sub2 , copy " & CopyNo
'End Sub2 ' End Copy 1 of Sub2
MsgBox Prompt:="Ending main procedure"
End Sub
Bookmarks