Next: , Previous: Subr Cells, Up: Data Types


6.1.5 Defining Subrs

If CCLO is #defined when compiling, the compiled closure feature will be enabled. It is automatically enabled if dynamic linking is enabled.

The SCM interpreter directly recognizes subrs taking small numbers of arguments. In order to create subrs taking larger numbers of arguments use:

— Function: make_gsubr name req opt rest fcn

returns a cclo (compiled closure) object of name char * name which takes int req required arguments, int opt optional arguments, and a list of rest arguments if int rest is 1 (0 for not).

SCM (*fcn)() is a pointer to a C function to do the work.

The C function will always be called with req + opt + rest arguments, optional arguments not supplied will be passed UNDEFINED. An error will be signaled if the subr is called with too many or too few arguments. Currently a total of 10 arguments may be specified, but increasing this limit should not be difficult.

          /* A silly example, taking 2 required args,
             1 optional, and a list of rest args */
          
          #include <scm.h>
          
          SCM gsubr_21l(req1,req2,opt,rst)
               SCM req1,req2,opt,rst;
          {
            lputs("gsubr-2-1-l:\n req1: ", cur_outp);
            display(req1,cur_outp);
            lputs("\n req2: ", cur_outp);
            display(req2,cur_outp);
            lputs("\n opt: ", cur_outp);
            display(opt,cur_outp);
            lputs("\n rest: ", cur_outp);
            display(rst,cur_outp);
            newline(cur_outp);
            return UNSPECIFIED;
          }
          
          void init_gsubr211()
          {
            make_gsubr("gsubr-2-1-l", 2, 1, 1, gsubr_21l);
          }