As I understand it ( and I'll be quickly corrected if I'm wrong ), here's how scope works.
In a CF page, all variables on that page (and <cfinclude>'d pages) have the same "Global" scope...functions included. By using the "var" keyword, you're telling CF to make the declared variable private scope of the function it was called in. Another scope issue that I've run across is that function arguments (<cfargument>) always have their own, unique scope and will not be overwritten in a recursive function call. -----Original Message----- From: Cedric Villat [mailto:[EMAIL PROTECTED] Sent: Thursday, September 18, 2003 1:13 PM To: CF-Talk Subject: Re:Recursion and Scope problem? Bingo, worked like a champ. But I'm still wondering WHY the scopes be overwritten? I just don't get it.... Cedric >Try using the "var" keyword in your first four <cfset> statements. > ><cfset var root_node = Arguments.node> ><cfset var levels = ... > > >-----Original Message----- >From: Cedric Villat [mailto:[EMAIL PROTECTED] >Sent: Thursday, September 18, 2003 12:59 PM >To: CF-Talk >Subject: Recursion and Scope problem? > > >Ok, I'm doing a bit of recursion, and my variables seem to be overwriting >themselves, which makes no sense to me. Here is my code: > ><cffunction name="getDTreeWidth" returntype="numeric"> > <cfargument name="node" type="string" required="yes"> > <cfargument name="level" type="numeric" required="yes"> > > <cfset root_node = Arguments.node> > <cfset levels = Arguments.level> > <cfset children = Request.children> > <cfset width = 0> > > <cfif levels EQ 0> > <!--- No more levels to show ---> > <cfset width = 1> > <cfelseif NOT StructKeyExists(children, root_node) OR >ArrayLen(children[root_node]) LTE 0> > <!--- No Children ---> > <cfset width = 1> > <cfelse> > <cfloop from="1" to="#numChildren(root_node)#" index="a"> > <cfset width = width + getDTreeWidth(children[root_node][a], levels - 1)> > </cfloop> > </cfif> > > <cfreturn width> ></cffunction> > > >In this example, the root_node is "Homer Simpson". He has 3 children Bart, >Lisa, and Maggie. When the function is first run, Arguments.node is "Homer >Simpson". It goes through the code until it gets to the recursive call >getDTreeWidth() which then calls itself for each child. After calling >getDTreeWidth() for Bart, it seems to work. When the function returns a >number and is added to the width, the next time through Homer Simpson's >loop, the root_node is now "Bart Simpson"! How could the recursive call to >Bart Simpson overwrite Homer Simpson's original arguments? Is this a bug or >am I missing something here? > >Any help would be appreciated. It may not make sense, so let me know if you >need some more clarification. > >Cedric > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| Message: http://www.houseoffusion.com/lists.cfm?link=i:4:137537 Archives: http://www.houseoffusion.com/lists.cfm?link=t:4 Subscription: http://www.houseoffusion.com/lists.cfm?link=s:4 Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4 This list and all House of Fusion resources hosted by CFHosting.com. The place for dependable ColdFusion Hosting. http://www.cfhosting.com