ByRef and ByVal in ColdFusion
Categories:
goog
ColdFusion
Pop Quiz:
Using only your brain (stop laughing!), mentally execute the following code and figure out what values the returned structure will contain:
Choices:

Early yesterday morning my choice would have been ‘B’. Late yesterday afternoon the answer was an obvious “D”. The reason is found in a little thing I like to call “ByRef” (Actually, the whole programming world calls it ByRef). ByRef stands for ‘By Reference’, and it is the polar opposite of ByVal (By Value).
Both of these terms refer to the way in which a value is passed along. You can equate it to the scenario of me giving you $100. If I hand you the cash, I have just given it to you “By Value”; you have it, it’s in your hand. If on the other hand I would have given you a check for $100, then I gave you the money “By Reference”; you don’t really have the money but you’re holding something that points to it.
ColdFusion by default passes structures by reference when you use them as a value. For instance, in the example of
(where stResults is a structure), my thisVar variable doesn’t actually have the stResults structure, but merely a pointer to the actual stResults. What this means is that if I modify stResults at some other place in my code and then go back to thisVar to get its value, thisVar’s value would also reflect the same changes. That’s really cool if you planned on things happening that way; not cool if you didn’t.
So how do we pass a structure by value and not by reference? Use the StructCopy function. Using the same example, if I set thisVar with
then modifying the stResults structure afterwards will not reflect within thisVar’s value. StructCopy then is the equivalent of taking a snapshot of the structure, capturing it as it is at that point in time. Consider the following illustration:

If we want the pop quiz code at the beginning of this article to actually produce the answer ‘B’, then we need to make one small modification to it, like so:
So, boys and girls, that is the difference between ByRef and ByVal. Not typically something we have to ever specify in CF, but DEFINATELY something we have to be aware of since it affects our code (and causes us unnecessary amounts of troubleshooting time).
Using only your brain (stop laughing!), mentally execute the following code and figure out what values the returned structure will contain:
<cfset listvar = "1,2,3,4">
<cfset stCounter = structNew() >
<cfset stResults = structNew()>
<cfloop list="#listvar#" index="i">
<cfset stCounter.item = i>
<cfset stResults[i] = stCounter>
</cfloop>
<cfset stCounter = structNew() >
<cfset stResults = structNew()>
<cfloop list="#listvar#" index="i">
<cfset stCounter.item = i>
<cfset stResults[i] = stCounter>
</cfloop>
Choices:

Early yesterday morning my choice would have been ‘B’. Late yesterday afternoon the answer was an obvious “D”. The reason is found in a little thing I like to call “ByRef” (Actually, the whole programming world calls it ByRef). ByRef stands for ‘By Reference’, and it is the polar opposite of ByVal (By Value).
Both of these terms refer to the way in which a value is passed along. You can equate it to the scenario of me giving you $100. If I hand you the cash, I have just given it to you “By Value”; you have it, it’s in your hand. If on the other hand I would have given you a check for $100, then I gave you the money “By Reference”; you don’t really have the money but you’re holding something that points to it.
ColdFusion by default passes structures by reference when you use them as a value. For instance, in the example of
<cfset thisVar = stResults>
(where stResults is a structure), my thisVar variable doesn’t actually have the stResults structure, but merely a pointer to the actual stResults. What this means is that if I modify stResults at some other place in my code and then go back to thisVar to get its value, thisVar’s value would also reflect the same changes. That’s really cool if you planned on things happening that way; not cool if you didn’t.
So how do we pass a structure by value and not by reference? Use the StructCopy function. Using the same example, if I set thisVar with
<cfset thisVar = StructCopy(stResults)>
then modifying the stResults structure afterwards will not reflect within thisVar’s value. StructCopy then is the equivalent of taking a snapshot of the structure, capturing it as it is at that point in time. Consider the following illustration:

If we want the pop quiz code at the beginning of this article to actually produce the answer ‘B’, then we need to make one small modification to it, like so:
<cfset listvar = "1,2,3,4">
<cfset stCounter = structNew() >
<cfset stResults = structNew()>
<cfloop list="#listvar#" index="i">
<cfset stCounter.item = i>
<cfset stResults[i] = StructCopy(stCounter)>
</cfloop>
<cfset stCounter = structNew() >
<cfset stResults = structNew()>
<cfloop list="#listvar#" index="i">
<cfset stCounter.item = i>
<cfset stResults[i] = StructCopy(stCounter)>
</cfloop>
So, boys and girls, that is the difference between ByRef and ByVal. Not typically something we have to ever specify in CF, but DEFINATELY something we have to be aware of since it affects our code (and causes us unnecessary amounts of troubleshooting time).

