int VEFCC vensim_external(
VV *val,
int nval,
int funcid)
val is the vector of arguments passed to the external function. It is a pointer to the union VV described below.
nval is the number of elements in val. If user loops is set, it will exceed the number of arguments set by user_definition.
funcid is the id of the function being called. Typically you will perform a switch on this to evaluate the function.
VV
typedef union { /* arguments are loaded on a vector and passed either */
COMPREAL val ; /* by value as floats or */
VECTOR_ARG *vec ; /* by address - normally the first element in an array */
TAB_TYPE *tab ;
char *literal ;
CONSTANT_MATRIX *constmat ;
DATA_MATRIX *datamat ;
} VV ;
VV is a union of the different argument types. If the function being called has num_loops > 0 then the first element will be VECTOR_ARG and contain values for the left hand side variable. If the function has num_loops == -1 (a Constant Definition function) the first argument will be a CONSTANT_MATRIX, and if num_loops == -2 is will be a DATA_MATRIX. (Remember that whenever num_loops is nonzero an additional argument will be passed first containing information about the left hand side variable).
If the function being called has num_literal > 0, then the next num_literal elements will be char *. If the function being called has num_lookup > 0, then the next num_lookup functions will be TAB_TYPE *. If the function has num_vector > 0 then the next num_vector elements will be VECTOR_ARG *. The remaining elements, if any will, be COMPREAL (float or double for the double precision version of Vensim).
It is often useful to pass integer values to function. To do this we recommend an explicit recast with rounding as in:
matrix_size = (int)(val[2].val + .5) ;
Return Value
If the function call is successful it should return 1. Return 0 in case of an error — this will be reported as an error in the simulation. The value computed by the function itself should be returned by assigning it to val[0].val. This is true even if val[0] was referencing a vector. Vensim makes no further use of val, except to reference val[0].val.