Author Topic: Optional Parameters  (Read 841 times)

How does one define optional parameters for a function? TheGeek kindly suggested the following code, yet it did not work.


Code: [Select]
function foo(%need,%not=5) { }

That would be valid in C-derived languages, but torque doesn't support optional parameters. You really don't need them though, because torque doesn't care about how many parameters there are:

Code: [Select]
function foo(%need, %not)
{
   if( %not $= "" )
   {
      // They didn't give a '%not' paramter
      // Or they put an empty string here
      echo("Optional parameter wasn't used.");
   }
   else
   {
      echo("Optional parameter was used.");
   }
}

The amount of parameters you provide doesn't matter:
Code: [Select]
foo("Hello"); // 'Optional parameter wasn't used'; the second param is set to: ""
foo("This", "is", "rather", "unnecessary"); // 'Optional parameter was used'
Extra parameters are ignored.
« Last Edit: June 09, 2008, 06:21:03 PM by exidyne »