Using recursive procedures

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Andre.

Hello,

could anyone post an example for the meaningful use of recursive procedures ?
Thanks in advance !


Regards
André

*** German PureBasic Support ***
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by freak.

Well here's one. It's a bit long, but it's the only one i used so far:

It creates a directorylisting of drive C:

Code: Select all

Structure WIN32_FIND_DATA_TMP
  dwFileAttributes.l
  ftCreationTime.FILETIME
  ftLastAccessTime.FILETIME
  ftLastWriteTime.FILETIME
  nFileSizeHigh.l
  nFileSizeLow.l
  dwReserved0.l
  dwReserved1.l
  cFileName.b[260]
  cAlternateFileName.b[14]
EndStructure
  
Declare SearchDisk (path.s)
  
MessageRequester("Scan","scanning drive C:",0)
  
  
CreateFile(0, "c:\scan.txt")
WriteStringN("Directory listing for Drive C:")
WriteStringN("******************************")
WriteStringN("")
  
SearchDisk("C:\")
  
CloseFile(0)
MessageRequester("Scan","done.",0)
End
  
  
Procedure SearchDisk(path.s)
  name.s = path + "*"
  handle.l = FindFirstFile_(name, info.WIN32_FIND_DATA_TMP)
  
  If handle  #INVALID_HANDLE_VALUE
    
    Repeat
      Entry.s = PeekS(@info\cFileName[0]) 
      If info\dwFileAttributes & #FILE_ATTRIBUTE_DIRECTORY
        If Entry  "." And Entry  ".."
          SearchDisk(path + Entry + "\")
          WriteStringN(path + Entry)
        EndIf
      EndIf
    Until FindNextFile_(handle, info)= #FALSE
      
    FindClose_(handle)
  EndIf
EndProcedure
 

Hope it helps...


Timo

--

A debugged program is one for which you have not yet found the conditions that make it fail.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fweil.

Hello,

Just to give one of the simplest regular example of recursive programing ... factorial : this is of low use but meaningful for understanding wariables usage and recursivity concept. This example affects the argument passed to the recursive function. Behind this you understand that you have to be careful using recursive coding to both the arguments use and the limits of recusive process (how to finish the loop).

You are welcome for more questions depending on context you would like to work on (like freak's touching data handling, or mine just making some calculation).

Procedure.l Fact(n.l)
If n > 1
n = Fact(n-1) * n
EndIf
ProcedureReturn n
EndProcedure

CallDebugger

Debug Fact(5)

Francois Weil
14, rue Douer
F64100 Bayonne
Post Reply