How to specify only some optional arguments when calling function in ColdFusion?

Kip picture Kip · Jul 1, 2009 · Viewed 10.8k times · Source

I have a ColdFusion function "foo" which takes three args, and the second two are optional:

<cffunction name="foo" access="public" returntype="any">
    <cfargument name="arg1" type="any" required="true" />
    <cfargument name="arg2" type="any" required="false" default="arg2" />
    <cfargument name="arg3" type="any" required="false" default="arg3" />

    ...

    <cfreturn whatever />
</cffunction>

I want to call foo, passing in arg1 and arg3, but leaving out arg2. I know that this is possible if I call the function using cfinvoke, but that syntax is really verbose and complicated. I have tried these two approaches, neither works:

<cfset somevar=foo(1, arg3=3) /> <!--- gives syntax error --->
<cfset somevar=foo(1, arg3:3) /> <!--- gives syntax error --->

Answer

Patrick McElhaney picture Patrick McElhaney · Jul 1, 2009

You have to use named arguments throughout. You can't mix named and positional arguments as you can in some other languages.

<cfset somevar = foo(arg1=1, arg3=3) />