This question came in today and I just had to answer it quickly: (Sorry to the 200 other people who have email in my queue!)
How do I return a value from a recursive function? This does not seem to work in cf. I'm basically walking up a hierarchy returning needed items when I find them.
Luckily s/he sent the code. The problem will probably jump out as soon as you see it:
<cffunction name="recursive" returntype="struct">
<cfargument name="id" type="numeric" required="yes" />
<cfset var stThing = structnew() />
<!--- do some lookup based on id ---> <cfif lookup succesful> <cfreturn stThing /> <cfelse> <!--- get parent id ---> <cfset recursive(parentid) /> </cfif>
Let’s trace this line by line to see where the error occurs.
- The first thing we do is create stThing as a new structure.
- Then we do a look up. This is the part of the code that is assumed.
- If the look up was successful, we return the value.
- If not.... and here comes the important part - we call the method again, but notice we don't set a value. Whenever you do, cfset foo(), which is allowed, you are basically running a function and ignoring the result.
So basically his code does call itself recursively, but each time the result gets "lost" in the ether and isn't returned to the user. If he changed the cfset to:
<cfreturn recursive(parentid)>It should work correctly. Recursion is like sushi - you need to be very careful when preparing it or the end result is crap (or even better, a server that loops until it crashes).