Skip to main content
BreakawayPaul
Inspiring
January 25, 2017
Answered

Optional Paramaters in CFScript Function

  • January 25, 2017
  • 1 reply
  • 5377 views

I usually use CFFUNCTION for things like this, but in this case I have existing code.  The code builds a self-expiring "New" tag for new (or updated) items on our website.  The code looks like this:

<cfscript>

    function newtag(type,date){

    if (type IS "n") msg = "New: ";

    else if (type IS "r") msg = "Revised: ";

    else msg = "Updated: ";

    if(datediff("d",date,now()) lt 90)

    WriteOutput(' <span class="hilite">' & msg & date & '</span>');

    }

</cfscript>

It works well, but it's hard-coded for 90 days.  While this works most of the time, I have occasionally been asked to extend it by 30 or 60 days.

I've tried adding a 3rd parameter for the expiry, like this:

   function newtag(type,date,exp){

But that ends up making the 3rd function required, and I don't want to have to sift through 70,000+ pages to update every place the function is called on.  Is there any way to include that 3rd argument and make it optional?  Or assign it a default value if it's not passed?

This topic has been closed for replies.
Correct answer WolfShade

You have a couple of options, here.

One is to set the arguments as required or optional inside the arguments area:

function myFunction(required any firstArg, string secondArg = ""){ // First argument is required; second is optional.

  .. blah blah blah ..

    }

Or, you could set the function with just one argument, but check argument length inside the function:

function myFunction(firstArg){

    secondArg = "";

    if(ArrayLen(ARGUMENTS) gt 1){ secondArg = ARGUMENTS[2]; }// If there is more than one argument, set second variable to value of second argument.

    .. blah blah blah..

    }

There might be another way, but I like these two, myself.

HTH,

^_^

1 reply

WolfShade
WolfShadeCorrect answer
Legend
January 25, 2017

You have a couple of options, here.

One is to set the arguments as required or optional inside the arguments area:

function myFunction(required any firstArg, string secondArg = ""){ // First argument is required; second is optional.

  .. blah blah blah ..

    }

Or, you could set the function with just one argument, but check argument length inside the function:

function myFunction(firstArg){

    secondArg = "";

    if(ArrayLen(ARGUMENTS) gt 1){ secondArg = ARGUMENTS[2]; }// If there is more than one argument, set second variable to value of second argument.

    .. blah blah blah..

    }

There might be another way, but I like these two, myself.

HTH,

^_^

BreakawayPaul
Inspiring
January 26, 2017

If I use your first method, I get "Variable exp undefined".  I thought exp might be reserved, but same error when I renamed it.  Going to try the second method now.

WolfShade
Legend
January 26, 2017

Hi, BreakawayPaul​,

On the one hand, I'm glad that you got it working; but on the other hand, I'm curious as to why my first example didn't work, for you.

The code I have on my DEV system is for a function that will truncate a string after x characters, without breaking words.  I won't post the whole function, but it's like:

<cfscript>

function abbrev(required string str,required numeric leng,string dontusethis=''){

  {code for abbreviating the string value}

}

</cfscript>

<cfoutput>#abbrev('this is a long string for testing purposes  this is a long string for testing purposes  this is a long string for testing purposes  this is a long string for testing purposes  this is a long string for testing purposes  ',100)#</cfoutput>

The above code works, for me.  No "undefined" messages.  What version of CF are you running?

V/r,

^_^

PS Thank you for marking my answer as correct.  I really appreciate it.