Using argumentCollection And Overriding Arguments

I ran some test code the other day just to make sure that I correctly understand how arguments passed in to functions in CFML work. If you know everything there is to know about functions and arguments in ColdFusion, then feel free not to read the rest of this. But, if you're curious...

Here is the code:

<cfoutput>
   <cffunction name="ExtraArgs" access="public" returntype="string">
      <cfargument name="Arg1" required="false" type="string" />
      <cfargument name="Arg2" required="false" type="string" />
      <cfargument name="Arg3" required="false" type="string" />
      <cfargument name="Arg4" required="false" type="string" />
      <cfif structKeyExists(arguments,'Arg4')>
         <cfreturn arguments.Arg4 />
      <cfelse>
         <cfreturn 'undefined' />
      </cfif>
   </cffunction>
   <cfset args3 = {Arg1='value1', Arg2='value2', Arg3='value3'} />
   <cfset args4 = {Arg1='value1', Arg2='value2', Arg3='value3', Arg4='value4'} />
   <br/> 1) #ExtraArgs(argumentCollection=args3)#                   <!--- output = "undefined" --->
   <br/> 2) #ExtraArgs(argumentCollection=args4)#                   <!--- output = "value4" --->
   <br/> 3) #ExtraArgs(argumentCollection=args4, Arg4='SomeOtherValue')# <!--- output = "SomeOtherValue" --->
</cfoutput>

All of the arguments are specified as 'not required' with no 'default' value. The function looks for the existence of the fourth argument and either returns that or the string "undefined".

The first time it is called with the "args3" struct that omits the "Arg4" argument, it returns "undefined".

The second time it is called with the "args4" struct, which contains "Arg4", it returns "value4" (the value of "Arg4" in the args4 struct).

The third time it is called with the "args4" struct and also the "Arg4" argument in addition to that, it returns "SomeOtherValue". This, I think, is the most interesting one. With this behavior, you could store structs containing the default arguments for certain operations, and then call those operations overriding defaults as needed.

Comments
BlogCFC was created by Raymond Camden. This blog is running version 5.6.002.