Functions
functions
The section describes how to define a function. In this chapter we describe functions that give information about functions, and various utility functions used either when defining functions or calling functions.

Information about a function
<#Include Label="NameFunction"> <#Include Label="NumberArgumentsFunction"> <#Include Label="NamesLocalVariablesFunction"> <#Include Label="FilenameFunc"> <#Include Label="StartlineFunc"> <#Include Label="PageSource">

Calling a function with a list argument that is interpreted as several arguments
<#Include Label="CallFuncList">

Functions that do nothing

The following functions return fixed results (or just their own argument). They can be useful in places when the syntax requires a function, but actually no functionality is required. So is often used as family predicate in .

<#Include Label="ReturnTrue"> <#Include Label="ReturnFalse"> <#Include Label="ReturnFail"> <#Include Label="IdFunc">

Function Types

Functions are &GAP; objects and thus have categories and a family.

<#Include Label="IsFunction"> <#Include Label="IsOperation"> <#Include Label="FunctionsFamily">

Naming Conventions

The way functions are named in &GAP; might help to memorize or even guess names of library functions.

If a variable name consists of several words then the first letter of each word is capitalized.

If the first part of the name of a function is a verb then the function may modify its argument(s) but does not return anything, for example appends the list given as second argument to the list given as first argument. Otherwise the function returns an object without changing the arguments, for example returns the concatenation of the lists given as arguments.

If the name of a function contains the word Of then the return value is thought of as information deduced from the arguments. Usually such functions are attributes (see ). Examples are , which returns a list of generators for the group entered as argument, or .

For the setter and tester functions of an attribute Attr the names SetAttr resp. HasAttr are available (see ).

If the name of a function contains the word By then the return value is thought of as built in a certain way from the parts given as arguments. For example, creating a group as a factor group of a given group by a normal subgroup can be done by taking the image of . Other examples of By functions are and .

Often such functions construct an algebraic structure given by its generators (for example, ). In some cases, By may be replaced by With (like e.g. ) or even both versions of the name may be used. The difference between StructByGenerators and StructWithGenerators is that the latter guarantees that the GeneratorsOfStruct value of the result is equal to the given set of generators (see ).

If the name of a function has the form AsSomething then the return value is an object (usually a collection which has the same family of elements), which may, for example: know more about its own structure (and so support more operations) than its input (e.g. if the elements of the collection form a group, then this group can be constructed using ); discard its additional structure (e.g. applied to a group will return a list of its elements); contain all elements of the original object without duplicates (like e.g. does if its argument is a list of elements from the same family); remain unchanged (like e.g. does if its argument is a group). If Something and the argument of AsSomething are domains, some further rules apply as explained in .

If the name of a function fun1 ends with NC then there is another function fun2 with the same name except that the NC is missing. NC stands for no check. When fun2 is called then it checks whether its arguments are valid, and if so then it calls fun1. The functions and are a typical example.

The idea is that the possibly time consuming check of the arguments can be omitted if one is sure that they are unnecessary. For example, if an algorithm produces generators of the derived subgroup of a group then it is guaranteed that they lie in the original group; would check this, and omits the check.

Needless to say, all these rules are not followed slavishly, for example there is one operation instead of two operations ZeroOfElement and ZeroOfAdditiveGroup.