To assign a function to the identifier NAME, with formal identifiers FORMAL_IDFIER, ..., FORMAL_IDFIER, use the syntax
NAME := function(FORMAL_IDFIER, ..., FORMAL_IDFIER) STATEMENTS end function;
or, if the return expression is easily specified:
NAME := func< FORMAL_IDFIER, ..., FORMAL_IDFIER | EXPRESSION >
To call this function within a statement, use the expression
NAME(ACTUAL, ..., ACTUAL)
When the function is called with actual values, Magma will return a value. In the func< ... > form, the value will be the value of EXPRESSION. In the function ... end function form, during the execution of the STATEMENTS Magma must encounter a statement
return EXPRESSION, ..., EXPRESSION;
so that a value or values can be returned from the function invocation, namely, the value of EXPRESSION(s).
> Fibonacci := func< n | n le 2 select 1 else $$(n-1) + $$(n-2) >; > f8 := Fibonacci(8); print f8; 21Given positive integer N, this returns s and d such that N - 1 = 2^s * d. > evenexp_odd := function(N) function> s := 0; d := N; function> q, r := Quotrem(d, 2); function> while r eq 0 do function|while> s +:= 1; function|while> d := q; function|while> q, r := Quotrem(d, 2); function|while> end while; function> return s, d; function> end function; > print evenexp_odd(2^8 * 35); 8 35