Community
SCI Programming => SCI Syntax Help => Topic started by: gumby on January 08, 2011, 08:51:10 AM
-
I'm trying to send an optional number of parameters to a procedure, and have unspecified parameters take on a default value in the procedure.
Apparently, you can pass as many (or few) parameters to a procedure that you want. Even more than the procedure has defined! However, if you choose not to pass a parameter, it's value in the procedure is set to a seemingly random reference in memory (I think) - definitely not NULL.
(procedure public (TestProc varA varB)
Print("test procedure")
)
...
TestProc()
TestProc(1)
TestProc(2 "test")
TestProc("troll" 29 32 "axe" 892)
All above calls to the procedure work just fine. Just don't reference parameters that weren't specified (you can, but who knows what you'll get). I did find a keyword called 'paramTotal' which can be referenced within a procedure that indicates how many parameters were passed in, which helps:
(procedure public (TestProc varA varB)
(if (== paramTotal 0)
Print("You passed in 0 parameters!")
)
(if (== paramTotal 1)
Print("You passed in 1 parameters!")
)
(if (== paramTotal 2)
Print("You passed in 2 parameters!")
)
)
I'm sure I could put something together for this using the 'paramTotal' variable; I just want to make sure that I haven't missing something in the documentation.
-
So here is how I resolved setting default values for non-passed parameters:
(procedure public (TestProcedure p_param1 p_param2 p_param3)
(var param1, param2, param3)
(if (>= paramTotal 1) = param1 p_param1)(else = param1 NULL)
(if (>= paramTotal 2) = param2 p_param2)(else = param2 44)
(if (>= paramTotal 3) = param3 p_param3)(else = param3 NULL)
...
)
I copied each parameter value into a local variable within the procedure. If a parameter is not passed, then the 'else' clause kicks in and sets the value of the variable to a 'default' value (in this example, either NULL or 44).
I don't reference the parameters again in procedure - instead I utilize the local variables for all comparisons & manipulations.