Author Topic: Procedure Parameters  (Read 1432 times)

0 Members and 1 Guest are viewing this topic.

Offline gumby

Procedure Parameters
« 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. 

Code: [Select]
(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:

Code: [Select]
(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.


In the Great Underground Empire (Zork port in development)
Winter Break 2012 Rope Prop Competition

Offline gumby

Re: Procedure Parameters
« Reply #1 on: January 09, 2011, 04:46:01 PM »
So here is how I resolved setting default values for non-passed parameters:

Code: [Select]
(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.
« Last Edit: January 09, 2011, 04:48:23 PM by gumby »
In the Great Underground Empire (Zork port in development)
Winter Break 2012 Rope Prop Competition


SMF 2.0.14 | SMF © 2017, Simple Machines
Simple Audio Video Embedder

Page created in 0.098 seconds with 23 queries.