Functions without parameters in CFML

I’ve been programming in the ColdFusion Markup Language (CFML) since 2000 and every now and again I find out something which I didn’t know, which always surprises me. Yesterday, while working on something I accidentally found out that in CFML you can pass parameters to a function without those parameters actually being defined in the parameter list of the function.

At first, I thought this might be a bug in Lucee, the CFML engine I was using, but after a conversation with the all knowledgeable Brad Wood from Ortus Solutions, it turns out this is actually a thing and has been in CFML, both Lucee and Adobe ColdFusion, for a long time.

Therefore, if you define a function with no parameters and then pass it parameters, either directly or as named parameters, these end up in the arguments scope, either numbered in the order in which they were passed or with the name given if passed as named parameters, as follows:

You can also mix and match this with parameters in the function definition, as follows:

Now, you might be thinking, how is this useful, well, I used it to create function that allows me to pass in parameters, which are then used as the input data to a REST service, like the following:

public function updateUser() {
    return this.sendRequest( '/user/' , 'PATCH' , serializeJSON( [ arguments ] ) );
}

In this case, the REST service updates a user and it only requires you to send the parameters that need updating, so when calling this function, instead of defining all the parameters and then having to loop over them and see which are empty and construct a data structure to pass to the REST service, I could just use the arguments scope directly as it has exactly what I need in it.

I’m not sure what other use cases there are, but I’m sure there are plenty and giving how long I’ve been doing CFML for and have never come across this, I’m sure others out there will not have either.

One Reply to “Functions without parameters in CFML”

  1. Thank you for the tip!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *