8 Replies Latest reply on Jan 27, 2017 7:56 AM by WolfShade

    Optional Paramaters in CFScript Function

    BreakawayPaul Level 2

      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?

        • 1. Re: Optional Paramaters in CFScript Function
          WolfShade Level 4

          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,

           

          ^_^

          • 2. Re: Optional Paramaters in CFScript Function
            BreakawayPaul Level 2

            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.

            • 3. Re: Optional Paramaters in CFScript Function
              BreakawayPaul Level 2

              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!!

              • 4. Re: Optional Paramaters in CFScript Function
                WolfShade Level 4

                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.

                • 5. Re: Optional Paramaters in CFScript Function
                  BreakawayPaul Level 2

                  We're running CF11.

                   

                  I tried something like this:

                   

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

                   

                  Perhaps I implemented it wrong?

                  • 6. Re: Optional Paramaters in CFScript Function
                    WolfShade Level 4

                    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,

                     

                    ^_^

                    • 7. Re: Optional Paramaters in CFScript Function
                      BreakawayPaul Level 2

                      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.

                      • 8. Re: Optional Paramaters in CFScript Function
                        WolfShade Level 4

                        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,

                         

                        ^_^