This section provides a very brief overview of the ideas behind functions, procedures and mappings.
The Magma language has a number of ways of writing routines that collect a quantity of programming steps together as one idea. They are functions and procedures, which are familiar from many programming languages, and mappings (including homomorphisms and partial mappings), which are familiar from mathematics.
Most Magma intrinsics, such as Abs(x), are functions. A few are procedures, such as ExtractRep(~S, ~x). Moreover, many standard Magma intrinsics return mappings, such as OrbitAction(G, T), and several constructors, such as quo, return mappings as one of their return values.
Functions, procedures and mappings can all be defined by the user too.
An important difference between mappings and the other two sorts of routines is that a mapping is specified as being from one algebraic structure to another, with a rule for calculating the image of any given element of the domain, whereas for functions and procedures there is no explicit declaration when they are being defined of the structures to which the arguments must belong.
When the routine has been defined, as an intrinsic or by the user, it can be invoked, or called. This involves matching up the formal arguments used in the definition with the inputs, known as the actual arguments. For functions and mappings, the arguments are value arguments, that is, they are fully evaluated before the routine starts computing with them. For procedures, if the arguments are preceded with a ~ symbol, as they are above, they are reference arguments, otherwise they are value arguments. Actual reference arguments must be given as identifiers, and these identifiers may change, according to how their formal parallels change.
Procedures are different from functions and mappings in that they do not return values when they are called, except in the sense that if the reference arguments change the context will alter.
A mapping must return a single value, an element of the codomain (though the domain may be a Cartesian product, with tuples as elements), but a function may return several values.
A call to an intrinsic function: > print Gcd(474, 255); 3A procedure definition and invocation: > SelectIntegers := procedure(~Q, b) procedure> if b then procedure|if> Q := [Q[i]: i in [1..#Q] | IsIntegral(Q[i])]; procedure|if> return; procedure|if> end if; procedure> Q:=[Q[i]: i in [1..#Q] | not IsIntegral(Q[i])]; procedure> end procedure; > > r := [6/8, 3, -8/4, 7, 36/5]; > SelectIntegers(~r, true); > print r; [ 3, -2, 7 ]
The natural homomorphism from S6 to S6/A6: > S6 := Sym(6); A6 := Alt(6); > Q, natural := quo< S6 | A6 >; > print natural; Mapping from: GrpPerm: S6 to GrpPerm: Q > print natural(S6!(5, 4)); (1, 2)