Extrinsic Functions

An extrinsic function is an M subroutine that another M routine can invoke to return a value.

The format for extrinsic functions is:

$$[label][^routinename]([expr|.lname[,...]])

M stacks $TEST for extrinsic functions. This is one of the two major differences between the DO command with parameters and extrinsics. On return from an extrinsic function, M restores the value of $TEST to what it was before the extrinsic function, regardless of the actions executed by the invoked routine.

M requires a routine that implements an extrinsic function to terminate with an explicit QUIT command which has an argument. M returns the value of the QUIT command argument as the value of the extrinsic function. This is the other major difference between the DO command with parameters and extrinsics. It is now possible to invoke a C function in a package via the external call mechanism.

Example:

POWER(V,X,S,T);extrinsic to raise to a power
;ignores fractional powers
SET T=1,S=0
IF X<0 SET X=-X,S=1
FOR X=1:1:X S T=T*V
QUIT $S(S:1/T,1:T)
GTM> WRITE $$^POWER(3,4)
81
GTM>
[Note] Note

The POWER routine uses a formallist that is longer than the "expected" actuallist to protect local working variables. Such practice may be encouraged or discouraged by your institution's standards.