Functions
The FUNCTION statement is used to create reusable bits of code which can be evaluated using function-call notation, and which may or may not return a value. FUNCTION statements are somewhat similar to macro facilities in legacy versions of CSL. However, functions work in a different way than macros. While a macro is really a template for creating additional model code in the location where the macro is invoked, a function does not create additional code. Rather, it works like typical functions defined in most modern programming languages:
- The FUNCTION statement defines a set of statements/equations that an be repeatedly called from various places in model code
- FUNCTION statements include the specification of one or more optional input arguments, as well as an optional returned value
- Any variable used as an argument to a FUNCTION must be declared within the FUNCTION itself. These variables are shared with the rest of the model code, so care must be taken to avoid naming conflicts.
- User-defined functions are invoked in exactly the same way built-in CSL functions (or operators) are invoked.
model FunctionExample
! Definition of the function (genralized logistic function)
! Note: for now, these variables are shared with the rest of the
! model, so need to be careful of name conflicts
function ss = genlog(aa, kk, bb, mm, nn, cc, tt)
dimension aa, kk, bb, mm, nn, cc, tt
ss = aa + (kk - aa)/((cc + exp(-bb*(tt - mm)))^(1/nn))
end
derivative
x1 = genlog(0, 1, 1, 5, 0.01, 1, t)
x2 = genlog(0, 1, 1, 5, 0.1, 1, t)
x3 = genlog(0, 1, 1, 5, 0.5, 1, t)
constant tstop = 20.0
termt(t >= tstop, 'Stopped on time limit')
end ! derivative
end ! program