• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Optional Paramaters in CFScript Function

Contributor ,
Jan 25, 2017 Jan 25, 2017

Copy link to clipboard

Copied

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?

Views

4.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jan 25, 2017 Jan 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 arg

...

Votes

Translate

Translate
LEGEND ,
Jan 25, 2017 Jan 25, 2017

Copy link to clipboard

Copied

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,

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jan 26, 2017 Jan 26, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 26, 2017 Jan 26, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jan 27, 2017 Jan 27, 2017

Copy link to clipboard

Copied

We're running CF11.

I tried something like this:

function newtag(msg,required date,numeric exp){  

Perhaps I implemented it wrong?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 27, 2017 Jan 27, 2017

Copy link to clipboard

Copied

Perhaps I implemented it wrong?

I believe so, yes.  It's difficult for me to figure out precisely what arguments you are attempting to use, but I'm going to guess msg, date, and exp.  First, I should state that it would be a bad idea to use any reserved words (like 'date') as argument names.  I'd suggest using something like newDate or submitDate.  I'll use 'newDate' for my example.

function newtag(required string msg, required date newDate, numeric exp=0){

This will require a string called 'msg', require a date called 'newDate', and set a default value of 0 to the optional numeric argument called 'exp'.

HTH,

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jan 27, 2017 Jan 27, 2017

Copy link to clipboard

Copied

Yeah re: the arguments being used.  date has been working for years, and I tried to rename exp with no better results.  But I didn't have the exp=0 (or = anything) in there, so maybe that's what was missing.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 27, 2017 Jan 27, 2017

Copy link to clipboard

Copied

LATEST

I believe that all required arguments go at the beginning/left, and must be declared 'required'; all optional arguments go at the end/right, and I _think_ must be given default values (haven't tried null - that might work.)

So, don't forget to declare the type of argument for each.  required string msg, required date newDate, numeric exp=0.

HTH,

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jan 26, 2017 Jan 26, 2017

Copy link to clipboard

Copied

Second method worked great!  Here's what I have now:

<cfscript>

    function newtag(arguments){

    numargs = arraylen(arguments);

    if (arguments[1] IS "n") msg = "New: ";

    else if (arguments[1] IS "r") msg = "Revised: ";

    else msg = "Updated: ";

    if(numargs eq 3) {

        exp1 = arguments[3] * 30;

    }

    else exp1 = 90;

    if(datediff("d",arguments[2],now()) lt exp1)

    WriteOutput(' <span class="hilite">' & msg & arguments[2] & '</span>');

    }

</cfscript>

Thanks!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation