CFLib.org – Common Function Library Project

OptArg(args, index[, default])

Last updated August 20, 2002

author

Jordan Clark

Version: 1 | Requires: CF5 | Library: UtilityLib

Description:
Easy way to define optional arguments for your functions. If the value isn't passed in, the local variable is still created.

Return Values:
Returns a value (any type).

Example:

<cfscript>
function foo( p1 ) {
    var p2 = optArg( arguments, 2, "foo" );
    var p3 = optArg( arguments, 3, "bar" );
    return p1 & p2 & p3;
}
</cfscript>
<cfoutput>#foo("moo")#</cfoutput>

Parameters:

Name Description Required
args Arguments Yes
index Index to check. Yes
default Value to use if args[index] doesn't exist. Defaults to an empty string. No

Full UDF Source:

/**
 * Pass in an optional UDF argument and define a default value at once.
 * 
 * @param args      Arguments (Required)
 * @param index      Index to check. (Required)
 * @param default      Value to use if args[index] doesn't exist. Defaults to an empty string. (Optional)
 * @return Returns a value (any type). 
 * @author Jordan Clark (JordanClark@Telus.net) 
 * @version 1, August 20, 2002 
 */
function OptArg( args, index ) {
    if( arrayLen( args ) GTE index ) {
        return args[ index ];
    } else if( arrayLen( arguments ) IS 3 ) {
        return arguments[ 3 ];
    } else {
        return "";
    }
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson