By Ref/By Value

Don’t forget that when you pass a struct, query, or CFC to a UDF, custom tag, or CFC method, you are passing it by reference, not by value. In English that means if you modify the value inside the udf/tag/method, you end up modifying the original as well. The best way to handle that is to simply use duplicate in your call:

cfset x = foo(duplicate(mystruct))

Of course, don’t forget that duplicate() will not work on a CFC instance. If you need to pass a CFC and NOT modify the original CFC, have the CFC itself define a duplicate or clone method and work on a real copy.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA