slang-users mailing list

[2001 Date Index] [2001 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

Re: Optional parameters in functions


Richard van Zon <rvanzon@xxxxxxxxxxxx> wrote:
>I am busy embedding S-Lang into my program, and I need a method to use
>optional parameters to my functions. Like :
>
>int add_a_to_b_and_maybe_c(int *a, int *b, [int *c])

You will have to use, e.g.,

   int add_a_to_b_and_maybe_c (void)
   {
      int a, b, c;
      
      c = 0;
      switch (SLang_Num_Function_Args)
      {
         case 3:
	   if (-1 == SLang_pop_integer (&c))
	     return -1;
	   /* drop */
	 case 2:
	   if ((-1 == SLang_pop_integer (&b))
	       || (-1 == SLang_pop_integer (&a)))
	     return -1;
	   
	   return a + b + c;
	   
	 default:
	   SLang_verror (SL_INVALID_PARM, "Expecting 2 or 3 arguments");
	   return -1;
      }

For other parameter types, e.g., char *a, use the appropriate
pop_function, e.g, SLang_pop_slstring (&a) and make sure that you free
the popped object with the appropriate free routine, e.g.,
SLang_free_slstring.

There are numerous examples of these techniques in the slang source
code.

--John
	   
	   


[2001 date index] [2001 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]