next up previous contents index
Next: Printing Up: Simple exercises in MAGMA Previous: if then statements   Contents   Index

MAGMA functions and procedures

A function is a procedure which returns something. (These things are called procedures in MAPLE.) Unlike MAPLE, you do not declare local or global variables.

Here's an example from the documentation [MAGMA]:

> fibonacci := function(n)
function>     if n le 2 then
function|if>        return 1;
function|if>     else
function|if>        return $$(n-1) + $$(n-2);
function|if>     end if;
function> end function;
> fibonacci(10)+fibonacci(12);
199

Here's an example of a silly procedure;

> procedure CheckEquality(x, y,~h)
procedure>      if x eq y then
procedure|if>          h := true;
procedure|if>      else
procedure|if>          h := false;
procedure|if>      end if;
procedure>  end procedure;
> CheckEquality(1,2,x);

>> CheckEquality(1,2,x);
                ^
Runtime error in procedure call: Argument 3 must be a variable reference (use ~)
> CheckEquality(1,2,~x);
> x;
false
You must use ~x in place of x in the third argument since it is an undeclared variable.

Exercise 7.2.16   Write a function which takes an integer input and returns ``even'' if it is even and ``odd'' otherwise.

Exercise 7.2.17  



David Joyner 2002-08-23