recursive question

1. Senior Member
Join Date
Dec 1969
Posts
226

## recursive question

Hi, I&#039;ve wrote a recursive function to count the number of someone&#039;s descendant. The function recurse well... but the number just didn&#039;t sum up right. Anyone find the problem here? Thanks.<BR><BR>function CountDescendant(arg_sName)<BR> Dim iNumDescendantReturn, iThisLevelNumber, sThisLevelName, sThisLevelSQL<BR> Dim rsNumDescendant<BR><BR> iNumDescendantReturn = 0<BR> sThisLevelName = arg_sName<BR><BR> sThisLevelSQL = "SELECT DISTINCT son_name " & _<BR> "FROM family " & _<BR> "WHERE father_name = &#039;" & sThisLevelName & "&#039; "<BR><BR> set rsNumDescendant = Server.CreateObject("ADODB.RecordSet")<BR> rsNumDescendant.Open sThisLevelSQL, connConnection, adOpenKeyset, adLockReadOnly<BR><BR> if not (rsNumDescendant.BOF and rsNumDescendant.EOF) then<BR> iThisLevelNumber = rsNumDescendant.RecordCount<BR> do while not rsNumDescendant.EOF<BR> iNumDescendantReturn = iThisLevelNumber + CountDescendant(rsNumDescendant.Fields("son_name") )<BR> rsNumDescendant.MoveNext<BR> loop<BR> end if<BR> rsNumDescendant.Close<BR> set rsNumDescendant = nothing<BR><BR> CountDescendant = iNumDescendantReturn<BR>end function<BR>

2. oli
Senior Member
Join Date
Dec 1969
Posts
3,961

## RE: recursive question

The numbers used inside the function to count &#039;this levels&#039; descendants should be &#039;local&#039; - as they are, but the overall total count var, should be global, so you should initialise it outsite the function, i.e:<BR><BR>var TotalDescendantCount = 0;<BR><BR>function....<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
226

## I see, thanks!

nt

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•