Use the "var" keyword when declaring your variables. Unfortunately, you
have to place all of your var declarations at the top of the function,
after the cfargument tags but before any other CFML.

<!--- this function is included via <cfinclude> from another page --->
<cffunction name="add_two_numbers">
  <cfargument name="first" required="true">
  <cfargument name="second" required="true">
  <cfset var sum=arguments.first + arguments.second>
  <cfreturn sum>
</cffunction>

Benjamin S. Rogers
http://www.c4.net/
v.508.240.0051
f.508.240.0057

-----Original Message-----
From: Jon Gunnip [mailto:[EMAIL PROTECTED]] 
Sent: Friday, January 24, 2003 9:57 AM
To: CF-Talk
Subject: Protecting scope of <cffunction> variables


Hello,

I am writing UDF's on MX using <cffunction>, and I would like to know
what 
the best way to create local variables for use inside my functions that
will 
not interfere with variables in the caller's scope.

Right now, I have to make sure that my variable names are unique inside
and 
outside function definitions.  This is quite burdensome, making it
difficult 
to easily name variables inside of functions to ensure I am not
overwriting 
variables in the callers scope.  It is also difficult for people to use
my 
functions, since beside knowing my function name, its arguments, and
what it 
returns, they must also know what variables I am using for internal
function 
processing to ensure that their variables are not overwritten.

Here is a basic example:

<!--- page function.cfm --->
<!--- this function is included via <cfinclude> from another page --->
<cffunction name="add_two_numbers">
  <cfargument name="first" required="true">
  <cfargument name="second" required="true">
  <cfset sum=arguments.first + arguments.second>
  <cfreturn sum>
</cffunction>


<!--- page that uses the function --->
<cfinclude template="functions.cfm">

<!--- using sum to track addition of numbers --->
<cfset sum=12>

<!--- sum is now 12 --->
<cfset result=add_two_numbers(1, 2)>
<!--- sum is now 3 (changed in function), when I want it to be 12 --->

<cfset sum=sum + result>
<!--- sum is now 6, when I want it to be 15 --->

I looked in the MM documentation, but I could not find an appropriate
answer 
to this question.

Thanks!
Jon



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4
Subscription: 
http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Structure your ColdFusion code with Fusebox. Get the official book at 
http://www.fusionauthority.com/bkinfo.cfm

                                Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
                                

Reply via email to