recusive example

conception_native_0123

Well-known member
Local time
Yesterday, 19:39
Joined
Mar 13, 2021
Messages
1,923
can someone here provide me example of this? i am trying to learn it, but only got this far using collection object
Code:
Sub testing()
Dim coll As New Collection
Dim a As Long
Dim b As Long
Dim c As Long
Dim d As Long
Dim counter As Long

a = 3
b = 2
c = 1
d = 4
  coll.Add (a)
  coll.Add (b)
  coll.Add (c)
  coll.Add (d)

    For counter = 1 To coll.Count
      Debug.Print coll.Item(counter)
    Next counter
End Sub
i would like to sort this collection using recursive function. can someone give me easy internet resource where i can learn? thank you.
 
you can use Array and sort the array.
if you google, there are lots of hits.
 
arnelgp

if i go here https://docs.microsoft.com/en-us/of...getting-started/creating-recursive-procedures

and i see the example i can write the print out but i dont think that is the right way to understand is it? i added a line but that is procedural i think. here is what i wrote
Code:
Function Factorial(N)
If N <= 1 Then ' Reached end of recursive calls.
Factorial = 1 ' (N = 0) so climb back out of calls.
Else ' Call Factorial again if N > 0.
Factorial = Factorial(N - 1) * N
Debug.Print Factorial
End If
End Function
and i got printed out this
Code:
?Factorial(5)
2
6
24
120
120
i understand the printout but i still dont think it is the right way to learn. is it? thank you
 
it cannot be right because this gives different answer
Code:
Function printout()
Debug.Print (5 - 1) * 5
End Function
 
it is a recursive (repeatedly calling itself).
go to that function and add a breakpoint (F9).
run the function, when the funcion get highlighted press F8 (step)
until the function finishes.
 
i did but hovering over function value always said it was empty. was i supposed to see something? this is what i saw

result.jpg
 
I find it easier to understand with a simpler example.
Heres a sample Db with a set of folders with subfolders.

The code without being recursive will just list all the folders in the root folder.

When you make it recursive, It is calling itself again for each folder or subfolder until there are no more and returns control to the original call. rinse and repeat.

Code:
Sub ListRecursive(strFolder As String, Lbx As ListBox, Optional IncludeSubFolders As Boolean = False)
'requires MS Scripting Runtime

    Dim fso As New FileSystemObject
    Dim Fld As Folder
    Dim fold As Folder

    Set Fld = fso.GetFolder(strFolder)

    For Each fold In Fld.SubFolders
        'Debug.Print fold.Name, fold.Path
        Lbx.AddItem fold.Name & ";" & fold.Path

        If IncludeSubFolders = True Then
            ListRecursive fold.Path, Lbx, IncludeSubFolders 'calls the sub again and again(recursive) until no more subfolders
        End If

    Next

End Sub
 

Attachments

thank you moke i will look and hopefully ask more question s soon. i appreciate really you uploading sample. :)
 
A very big "gotcha" to remember is that in recursive functions, the sense of argument passage (ByVal or ByRef) can make a HUGE difference.

Things passed ByRef will be shared with other iterations of your recursive routine. If a ByRef variable is updated, it will be altered when you exit the iteration of that code and return to a previous instance thereof.

Things passed ByVal are copies for which the actual variable can't be altered in the recursion. That factorial example in post #6 only works when N is passed ByVal.

This is also why you HAVE to be careful when using public variables defined externally to the recursive routine. Variables defined locally in the recursion are not shared with other iterations of the same routine; they are defined on the program stack and exist only for the duration of that specific iteration. Things defined public/global to that routine, however, don't go away. Therefore, be VERY careful with variables in recursive code.
 
The first example trimspaces is recursive
This function uses recursion to change all double-spaces in a string into a single space character.
 
I think you may have some confusion with recursion and something else. You keep mentioning sorting algorithms which I have never seen done recursively. Maybe your nomenclature is wrong.

Recursive functions are very easy to spot. They call themselves.

Code:
Public Function TrimSpaces(str As String) As String
'Converts all double spaces into a single space character.
'4/7/2013
'Source: http://www.accessmvp.com/thedbguy

If InStr(str, "  ") > 0 Then
    str = TrimSpaces(Replace(str, "  ", " "))
End If

TrimSpaces = str

End Function

Inside the function it calls itself
str = TrimSpaces(Replace(str, " ", " "))
 
Last edited:
Code:
Public Function TrimSpaces(str As String) As String
'Converts all double spaces into a single space character.
'4/7/2013
'Source: http://www.accessmvp.com/thedbguy

If InStr(str, "  ") > 0 Then
    str = TrimSpaces(Replace(str, "  ", " "))
End If

TrimSpaces = str

End Function

Inside the function it calls itself
str = TrimSpaces(Replace(str, " ", " "))

i guess what i need is connection between the following

input value into function and how it goes from in to final out
why iteration is so important is recursive functions and what it does (maybe graph or visual to look at?)
number one purpose of calling itself
 
Sorry, No idea what you are saying. If english is not your native language try using google translate. None of what you said means anything in english
 
Sorry, No idea what you are saying. If english is not your native language try using google translate. None of what you said means anything in english

I like know why it important for recursion:

1) how does function's input value change. why? every time it goes through the function.
2) a picture showing how storage memory changes during each iteration of function.

thank you

 
Last edited:
why iteration is so important is recursive functions and what it does (maybe graph or visual to look at?)
number one purpose of calling itself
Thats what I was trying to get accross in my example using folders.

The code goes through a folder to get the name of each folder in that folder. So in RootFolder it finds a folder named Sub1A. Before the procedure moves on to find the next folder in RootFolder, it calls itself to find all the folders in Sub1A. So in Sub1A it finds Sub1B. It then calls itself again to find all the folders in Sub1B. It will do this until it no longer finds subfolders in a folder and then it returns to where it left off in the RootFolder and moves on to the next folder which in my example is Sub2A. It will continue to call itself this way until it finds no more folders.
 

Users who are viewing this thread

Back
Top Bottom