Fname \{ Snt1; Snt2; ... Sntn; }; Fname { Snt1; Snt2; ... Sntn; };
where Fname is the name of the function being defined, and Snt1, Snt2, ..., Sntn are sentences. (Being, at present, of no importance, the subtle difference between \{ and { will be explained later.)
<Fname Re>
to the function Fname is to be evaluated. Then the result expression Re is evaluated. If a ground expression Ge is returned, an attempt is made to match Ge against the input patterns P1, P2, ..., Pn, in order to find the first pattern Pj such that matching Ge against P succeeds. Let Env be the "first" variant of matching Ge against P. Then the rest Rj is evaluated in the environment Env. If a ground expression Ge' is returned, this expression is taken to be the result of evaluating the function call.
= Rej
where Rej is a result expression. A rest of the form = Re is a special case of right hand side. Evaluating a right hand side = Rej amounts to evaluating the result expression Rej. If the evaluation of Rej results in returning a ground expression Ge, then Ge is taken to be the result of the whole right hand side.
SumSq(X,Y) = X*X + Y*Ywhich may be rewritten in Refal in the following way:
$func SumSq sX sY = sZ; SumSq { sX sY = <Add <Mult sX sX> <Mult sY sY>>; };
It should be noted that the declaration of a function must precede the function's definition as well as the calls to the function, since the information provided by the declaration is necessary for compiling the function's definition as well as the calls to the function.
$func Fname Fin = Fout;the compiler verifies that the input patterns P1, P2, ..., Pn are "instances" of the input format Fin, whereas all the rests R1, R2, ..., Rn are certain to return ground expressions satisfying the output format Fout.