views:

261

answers:

4

By default, ColdFusion passes simple types (like numeric, string, and GUID) by value to functions. I'd like to pass a simple type by reference.

I'm currently wrapping a simple value in a struct (they get passed by reference). This solves my problem but it is very ugly:

<!--- TheFunctionName---->
<cffunction name="TheFunctionName">
     <cfargument name="OutVariable" type="struct">
     <cfset OutVariable.ID = 5>
</cffunction>

<cfset OutVariable=StructNew()>
<cfset TheFunctionName(OutVariable)>

<!--- I want this to output 5--->
<cfoutput>#OutVariable.ID#</cfoutput>

I'd rather something like this:

<!--- TheFunctionName---->
<cffunction name="TheFunctionName">
     <cfargument name="OutVariable" passbyref="true">
     <cfset OutVariable = 5>
</cffunction>

<cfset TheFunctionName(OutVariable)>

<!--- I want this to output 5--->
<cfoutput>#OutVariable#</cfoutput>
+6  A: 

AFAIK, there's no way to pass simple values by reference in ColdFusion. The only workaround I can think of is the one you're already using.

Instead, I would suggest trying to restructure your program to work with the grain of the language. In cases where there's only one simple value to "modify", you could just make your function return the new value, and call it like:

<cfset SomeVar = TheFunctionName(SomeVar)>

In cases where you're modifying multiple values, take a step back and think about whether it's possible to bundle those multiple values up into a CFC with your mutator functions becoming methods of the CFC. This could be clearer and more maintainable solution anyway.

Jason Creighton
+1  A: 

You can arrange for the variables used outside and inside the function to be in a scope that exists in both code areas. For example, if you put a variable in the "session" or the "request" scope you will be able to access it from within the function. The changes made will persist.

Note that when you are doing this you aren't actually "passing" the variables to the function. The function just assumes the variable exists or creates it, depending on how you code it.

<cffunction name="TheFunctionName">
     <cfset Request.StrVar = "inside function<br />" />
</cffunction>

<cfscript>
    Request.StrVar = "outside function<br />";
    WriteOutput(Request.StrVar);
    TheFunctionName();
    WriteOutput(Request.StrVar);
</cfscript>

About ColdFusion Scopes

If there is any doubt about the calling page declaring the variable in advance when it is required you'll have to do some legwork with the <cfparam> tag or IsDefined() function.

SurroundedByFish
+1  A: 

If you:

  1. declare the function inside of a CFC
  2. invoke the function using <cfinvoke>

You would be able to specify the <cfinvoke> parameter "returnvariable", and then output that variable however you like.

<cfinvoke component="this" method="TheFunctionName" returnvariable="blah">
     <cfinvokeargument name="data" value="whatever" type="string">

     <cfreturn data>
</cfinvoke>

<cfdump var="#blah#">

If you are writing everything in cfscript, then I would go with what SurroundedByFish said.

Jason
A: 

Coldfusion is a very simple & basic language...

If you want a real Web language, go for Java or Php or .Net!

By the way, now this is deprecated in Php...

Coldfusion is not free, no big community, no lot of MVC frameworks and support... CF should be never used ;-)

Tom
Coldfusion is a domain specific language built for web development. It is a REAL web language.
Brian Bolton
CF (language) is free and Open, CF has a quality community, there are countless CF MVC frameworks, and plenty of CF support options.Now stop flaming and go do something constructive.
Peter Boughton