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 "";
	}
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson