«

»

Mar 23

Default Values for Argument Arrays

One of the nice things about arrays in programming is that they allow you to infinity increase the argument limit of a function when you are able to pass them. ie in PHP.

function myFunction($arg1)
{
	var_dump($arg1);
}

myFunction(array("argument1","argument2"));
myFunction(array("argument1","argument2","argument3","argument4","argument5"));

if you tried to call the function without specifying the argument you’d get a warning and notice in PHP, in some stricter languages it’ll be a compile error. but consider when you set default values for arguments

function myFunction($arg1 = "test", $arg2 = "test 2")
{
	var_dump($arg1);
	var_dump($arg2);
}
myFunction();
myFunction("argument1");

function myFunction2($arg1 = array("arg1" => "test", "arg2" => "test 2"))
{
	var_dump($arg1);
}
myFunction2();
myFunction2(array("arg1" => "argument1"));

unlike in our first function where our second argument’s default value is still shown in our second function we loose it. this is because the default only works if nothing was passed so since we did pass an array to the argument (without our second argument) we no longer have the default second argument.

however there is a way to set up default values for an argument array. it’s more or less doing an isset check for the entire array

function myFunction($arg1 = array())
{
	$defaultArray =  array("arg1" => "test", "arg2" => "test 2");
	
	foreach($defaultArray as $key1 => $value1)
	{
		if(!isset($template[$key1]))
		{
			$arg1[$key1] = $value1;
		}
	}
	
	var_dump($arg1);
}

myFunction();
myFunction(array("arg2" => "hello"));

As you can see we see we have moved out default array outside the function header and into the body and using this we check if the values of the default exist in our passed argument and if not set it the default value. if you run the code you’ll notice that we didn’t include arg1 but arg2. this is nifty because as far as i know you can’t just go myFunction("value for second argument") where you skip the first argument and assign something to the second. with this argument array you can, in fact the order the arguments are passed doesn’t matter at all (at least when you call the function, if they need to be in a certain order for a loop you’d best work that out)

What about other languages?

So as with all my code what you should be taking is the logic behind this as logic can be applied to all languages and with enough research and knowledge you can duplicate functions and functionality in any language (with some exceptions)

In some languages where you have to set the size of the array when you define the variable and use something like ReDim to alter the size in these situations rather than doing multiple redefinitions it would probably be better to update the values in the defaultArray.

this also is very useful in Javascript as in javascript you can’t set default values for your arguments and in order to simulate this you have to check if each argument to see if it’s undefined. in an array it’s easier and with less code because you have a loop

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>