Default function parameters

Out of curiosity, does anyone know if Quest supports default function parameters? I looked through the documentation and forums and don't see it mentioned anywhere. From what I can tell there doesn't seem to be a way to call a function without passing an argument for each parameter, but I don't know enough about Quest to say for certain.


If you call a function in an expression, parameters can be optional.

So if you do:

MyFunction ("parameter 1", parameter 2)

you have to specify exactly the number of parameters in the function definition.

but if you do:

some_variable = MyFunction ("Only one parameter even though this function expects 2")

that isn't actually an error.

If you're writing a function where you intend some parameters to be optional, you would start with something like:

if (not IsDefined ("second_param")) {
  second_param = "Default value"
}

(IsDefined's parameter is a string - the name of a variable to test the existence of. It works for any local variables, which includes function parameters)

If you want more flexible functions, it's possible to check the types of the parameters that are passed and rearrange them, so you can allow any of the parameters to be omitted. But usually there's no need to do this, and it's easier to pass null or some other special value which the function recognises and replaces with the default.


Thanks! That's good to know. I'm not sure if I'll ever need to do this, but I was testing some ideas and wondered if it was possible. Now I'll have it in my toolbox should I ever want to use it.

Cheers!


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums