Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > 5e1854624d3bc613bdd0dd13d1ef9ac7 > files > 146

gap-system-4.4.12-5mdv2010.0.i586.rpm

<html><head><title>[ref] 4 The Programming Language</title></head>
<body text="#000000" bgcolor="#ffffff">
[<a href="../index.htm">Top</a>] [<a href = "chapters.htm">Up</a>] [<a href ="CHAP003.htm">Previous</a>] [<a href ="CHAP005.htm">Next</a>] [<a href = "theindex.htm">Index</a>]
<h1>4 The Programming Language</h1><p>
<P>
<H3>Sections</H3>
<oL>
<li> <A HREF="CHAP004.htm#SECT001">Language Overview</a>
<li> <A HREF="CHAP004.htm#SECT002">Lexical Structure</a>
<li> <A HREF="CHAP004.htm#SECT003">Symbols</a>
<li> <A HREF="CHAP004.htm#SECT004">Whitespaces</a>
<li> <A HREF="CHAP004.htm#SECT005">Keywords</a>
<li> <A HREF="CHAP004.htm#SECT006">Identifiers</a>
<li> <A HREF="CHAP004.htm#SECT007">Expressions</a>
<li> <A HREF="CHAP004.htm#SECT008">Variables</a>
<li> <A HREF="CHAP004.htm#SECT009">More About Global Variables</a>
<li> <A HREF="CHAP004.htm#SECT010">Function Calls</a>
<li> <A HREF="CHAP004.htm#SECT011">Comparisons</a>
<li> <A HREF="CHAP004.htm#SECT012">Arithmetic Operators</a>
<li> <A HREF="CHAP004.htm#SECT013">Statements</a>
<li> <A HREF="CHAP004.htm#SECT014">Assignments</a>
<li> <A HREF="CHAP004.htm#SECT015">Procedure Calls</a>
<li> <A HREF="CHAP004.htm#SECT016">If</a>
<li> <A HREF="CHAP004.htm#SECT017">While</a>
<li> <A HREF="CHAP004.htm#SECT018">Repeat</a>
<li> <A HREF="CHAP004.htm#SECT019">For</a>
<li> <A HREF="CHAP004.htm#SECT020">Break</a>
<li> <A HREF="CHAP004.htm#SECT021">Continue</a>
<li> <A HREF="CHAP004.htm#SECT022">Function</a>
<li> <A HREF="CHAP004.htm#SECT023">Return</a>
<li> <A HREF="CHAP004.htm#SECT024">The Syntax in BNF</a>
</ol><p>
<p>
This chapter describes the <font face="Gill Sans,Helvetica,Arial">GAP</font> programming language.  It should allow
you in principle to predict the result of each and every input. In order
to know what we are talking about, we first have to look more closely at
the process of interpretation and the various representations of data
involved.
<p>
<p>
<h2><a name="SECT001">4.1 Language Overview</a></h2>
<p><p>
First we have the input to <font face="Gill Sans,Helvetica,Arial">GAP</font>, given as a string of characters. How
those characters enter <font face="Gill Sans,Helvetica,Arial">GAP</font> is operating system dependent, e.g., they
might be entered at a terminal, pasted with a mouse into a window, or
read from a file. The mechanism does not matter. This representation of
expressions by characters is called the <strong>external representation</strong> of the
expression. Every expression has at least one external representation
that can be entered to get exactly this expression.
<p>
The input, i.e., the external representation, is transformed in a process
called <strong>reading</strong> to an internal representation.  At this point the input
is analyzed and inputs  that are not legal external representations,
according to the rules given below, are rejected as errors. Those rules
are usually called the <strong>syntax</strong> of a programming language.
<p>
The internal representation created by reading is called either an
<strong>expression</strong> or a <strong>statement</strong>.  Later we will distinguish between those
two terms.  However for now we will use them interchangeably.
The exact form of the internal representation does not matter.
It could be a string of characters equal to the external representation,
in which case the reading would only need to check for errors.
It could be a series of machine instructions for the processor on which
<font face="Gill Sans,Helvetica,Arial">GAP</font> is running, in which case the reading would more appropriately
be called compilation.
It is in fact a tree-like structure.
<p>
After the input has been read it is again transformed in a process called
<strong>evaluation</strong> or <strong>execution</strong>. Later we will distinguish between those two
terms too, but for the moment we will use them interchangeably. The name
hints at the nature of this process, it replaces an expression with the
value of the expression. This works recursively, i.e., to evaluate an
expression first the subexpressions are evaluated and then the value of
the expression is computed from those values according to rules given below.
Those rules are usually called the <strong>semantics</strong> of a programming language.
<p>
The result of the evaluation is, not surprisingly, called a <strong>value</strong>.
Again the form in which such a  value is
represented internally does not  matter. It is  in fact a tree-like
structure again.
<p>
The last process is called <strong>printing</strong>. It takes the value produced by
the evaluation and creates an external representation, i.e., a string of
characters again. What you do with this external representation is up to
you. You can look at it, paste it with the mouse into another window, or
write it to a file.
<p>
Lets look at an example to make this more clear. Suppose you type in the
following string of 8 characters
<p>
<pre>
1 + 2 * 3;
</pre>
<p>
<font face="Gill Sans,Helvetica,Arial">GAP</font> takes  this external representation  and creates  a tree-like
internal representation, which we can picture as follows
<p>
<pre>
  +
 / \
1   *
   / \
  2   3
</pre>
<p>
This expression is then evaluated. To do this <font face="Gill Sans,Helvetica,Arial">GAP</font> first evaluates the
right subexpression <code>2*3</code>.  Again, to do this <font face="Gill Sans,Helvetica,Arial">GAP</font> first evaluates its
subexpressions 2 and 3. However they are so simple that they are their
own value, we say that they are self-evaluating. After this has been
done, the rule for <code>*</code> tells us that the value is the product of the
values of the two subexpressions, which in this case is clearly 6.
Combining this with the value of the left operand of the <code>+</code>, which is
self-evaluating, too, gives us the value of the whole expression 7. This
is then printed, i.e., converted into the external representation
consisting of the single character <code>7</code>.
<p>
In this fashion we can predict the result of every input when we know the
syntactic rules that govern the process of reading and the semantic rules
that tell us for every expression how its value is computed in terms of
the values of the subexpressions. The syntactic rules are given in
sections <a href="CHAP004.htm#SECT002">Lexical Structure</a>,  <a href="CHAP004.htm#SECT003">Symbols</a>, <a href="CHAP004.htm#SECT004">Whitespaces</a>, <a href="CHAP004.htm#SECT005">Keywords</a>,
<a href="CHAP004.htm#SECT006">Identifiers</a>, and <a href="CHAP004.htm#SECT024">The Syntax in BNF</a>, the semantic rules are given in
sections <a href="CHAP004.htm#SECT007">Expressions</a>, <a href="CHAP004.htm#SECT008">Variables</a>, <a href="CHAP004.htm#SECT010">Function Calls</a>, <a href="CHAP004.htm#SECT011">Comparisons</a>,
<a href="CHAP004.htm#SECT012">Arithmetic Operators</a>,  <a href="CHAP004.htm#SECT013">Statements</a>, <a href="CHAP004.htm#SECT014">Assignments</a>, <a href="CHAP004.htm#SECT015">Procedure Calls</a>,
<a href="CHAP004.htm#SECT016">If</a>, <a href="CHAP004.htm#SECT017">While</a>, <a href="CHAP004.htm#SECT018">Repeat</a>, <a href="CHAP004.htm#SECT019">For</a>, <a href="CHAP004.htm#SSEC022.1">Function</a>, and the chapters describing
the individual data types.
<p>
<p>
<h2><a name="SECT002">4.2 Lexical Structure</a></h2>
<p><p>
Most input of <font face="Gill Sans,Helvetica,Arial">GAP</font> consists of sequences of the following characters.
<p>
Digits, uppercase and lowercase letters, <var>space</var>, <var>tab</var>, <var>newline</var>, <var>return</var>
and the special characters
<p>
<pre>
"    `    (    )    *    +    ,    -    #
.    /    :    ;    &lt;    =    &gt;    ~    
[    \    ]    ^    _    {    }    ! 
</pre>
<p>
It is possible to use other characters in identifiers by escaping
them with backslashes, but we do not recommend to use this feature.
Inside strings
(see section&nbsp;<a href="CHAP004.htm#SECT003">Symbols</a> and chapter&nbsp;<a href="CHAP026.htm">Strings and Characters</a>) and
comments (see&nbsp;<a href="CHAP004.htm#SECT004">Whitespaces</a>) the full character set supported by
the computer is allowed.
<p>
<p>
<h2><a name="SECT003">4.3 Symbols</a></h2>
<p><p>
The process of reading, i.e., of assembling the input into expressions,
has a subprocess, called <strong>scanning</strong>, that assembles the characters into
symbols.  A <strong>symbol</strong> is a sequence of characters that form a lexical
unit. The set of symbols consists of keywords, identifiers, strings,
integers, and operator and delimiter symbols.
<p>
A <strong>keyword</strong> is a reserved word (see <a href="CHAP004.htm#SECT005">Keywords</a>). An <strong>identifier</strong> is 
a sequence of letters, digits and underscores (or other characters
escaped by backslashes) that
contains at least one non-digit and is not a keyword (see <a href="CHAP004.htm#SECT006">Identifiers</a>).
An integer is a sequence of digits (see <a href="CHAP014.htm">Integers</a>), possibly prepended 
by <code>-</code> and <code>+</code> sign characters.  A <strong>string</strong> is a
sequence of  arbitrary characters  enclosed in double quotes (see
<a href="CHAP026.htm">Strings and Characters</a>).
<p>
Operator and delimiter symbols are
<p>
<pre>
+    -    *    /    ^    ~   !.
=    &lt;&gt;   &lt;    &lt;=   &gt;    &gt;=  ![
:=   .    ..   -&gt;   ,    ;   !{
[    ]    {    }    (    )    :
</pre>
<p>
Note also that during the process of scanning all whitespace is removed
(see <a href="CHAP004.htm#SECT004">Whitespaces</a>).
<p>
<p>
<h2><a name="SECT004">4.4 Whitespaces</a></h2>
<p><p>
<a name = "I0"></a>

<a name = "I1"></a>

<a name = "I1"></a>
<a name = "I2"></a>

<a name = "I1"></a>
<a name = "I2"></a>
<a name = "I3"></a>

<a name = "I1"></a>
<a name = "I2"></a>
<a name = "I3"></a>
<a name = "I4"></a>

The  characters <var>space</var>, <var>tab</var>,  <var>newline</var>, and  <var>return</var> are called
<strong>whitespace characters</strong>.  Whitespace is used  as necessary to separate
lexical symbols, such as integers, identifiers, or keywords. For example
<code>Thorondor</code> is a single identifier, while <code>Th or ondor</code> is the keyword
<code>or</code> between the two identifiers <code>Th</code> and <code>ondor</code>. Whitespace may occur
between any two symbols, but not within a symbol. Two or more adjacent
whitespace characters are equivalent to a single whitespace.
Apart from the role as separator of symbols,
whitespace characters are  otherwise insignificant.
Whitespace characters may also occur inside a string,
where they are significant.
Whitespace characters should also be used freely for improved readability.
<p>
A <strong>comment</strong> starts with the  character <code>#</code>, which is sometimes called
sharp or hatch, and continues to the end of the line on which the comment
character appears. The whole comment, including <code>#</code> and the <var>newline</var>
character is treated as a single  whitespace. Inside a string, the
comment character <code>#</code> loses its role and is just an ordinary character.
<p>
For example, the following statement
<p>
<pre>
if i&lt;0 then a:=-i;else a:=i;fi;
</pre>
<p>
is equivalent to
<p>
<pre>
if i &lt; 0 then   # if i is negative
  a := -i;      #   take its additive inverse
else            # otherwise
  a := i;       #   take itself
fi;
</pre>
<p>
(which by the way shows that it is possible to write superfluous
comments). However the first statement is <strong>not</strong> equivalent to
<p>
<pre>
ifi&lt;0thena:=-i;elsea:=i;fi;
</pre>
<p>
since the keyword <code>if</code> must be separated from the identifier <code>i</code> by a
whitespace, and similarly <code>then</code> and <code>a</code>, and <code>else</code> and <code>a</code> must be
separated.
<p>
<p>
<h2><a name="SECT005">4.5 Keywords</a></h2>
<p><p>
<strong>Keywords</strong> are reserved words that are used to denote special operations
or are part of statements. They must not be used as identifiers. The
keywords are
<p>
<pre>
and     do       elif   else    end     fi
for     function if     in      local   mod
not     od       or     repeat  return  then
until   while    quit   QUIT    break   rec 
continue
</pre>
<p>
Note that (almost) all keywords are written in lowercase and that they
are case sensitive. For example only <code>else</code> is a keyword; <code>Else</code>,
<code>eLsE</code>, <code>ELSE</code> and so forth are ordinary identifiers. Keywords must
not contain whitespace, for example <code>el if</code> is not the same as <code>elif</code>.
<p>
Note: A number of tokens that appear to be normal identifiers
representing functions or literals of various kinds are actually
implemented as keywords for technical reasons. The only consequence of
this is that those identifiers cannot be re-assigned, and do not
actually have function objects bound to them, which could be assigned
to other variables or passed to functions. These keywords are:
<p>
<pre>
false 	true	IsBound	Unbind	TryNextMethod	
Info	Assert	SaveWorkspace   fail
</pre>
<p>
<p>
<h2><a name="SECT006">4.6 Identifiers</a></h2>
<p><p>
An <strong>identifier</strong> is used to refer to  a variable (see <a href="CHAP004.htm#SECT008">Variables</a>).  An
identifier usually consists of letters, digits, and underscores <code>_</code>, and must
contain at least one non-digit.  An identifier is terminated
by the first character not in this class. Examples of valid identifiers
are
<p>
<pre>
a           foo         aLongIdentifier
hello       Hello       HELLO
x100        100x       _100
some_people_prefer_underscores_to_separate_words
WePreferMixedCaseToSeparateWords
</pre>
<p>
Note that case is significant, so the three identifiers in the second
line are distinguished.
<p>
The backslash <code>\</code> can be used to include other characters in identifiers;
a backslash followed by a character is equivalent  to the character,
except that this escape sequence is considered to be an ordinary letter.
For example
<pre>
G\(2\,5\)
</pre>
is an identifier, not a call to a function <code>G</code>.
<p>
An identifier that starts with a backslash is never a keyword, so for
example <code>\*</code> and <code>\mod</code> are identifiers.
<p>
The length of identifiers is not limited,  however only the first 1023
characters are significant. The escape sequence <code>\</code><var>newline</var> is ignored,
making it possible to split long identifiers over multiple lines.
<p>
<a name = "SSEC006.1"></a>
<li><code>IsValidIdentifier( </code><var>str</var><code> ) F</code>
<p>
returns <code>true</code>  if  the  string  <var>str</var>  would  form  a  valid  identifier
consisting of letters,  digits  and  underscores;  otherwise  it  returns
<code>false</code>. It does not check whether <var>str</var> contains characters escaped by a
backslash <code>\</code>.
<p>
<p>
<h2><a name="SECT007">4.7 Expressions</a></h2>
<p><p>
<a name = "I5"></a>

An <strong>expression</strong> is a construct that evaluates to a value.  Syntactic
constructs that are executed to produce a side effect and return no value
are called <strong>statements</strong> (see <a href="CHAP004.htm#SECT013">Statements</a>). Expressions appear as right
hand sides of assignments (see <a href="CHAP004.htm#SECT014">Assignments</a>), as actual arguments in
function calls (see <a href="CHAP004.htm#SECT010">Function Calls</a>), and in statements.
<p>
Note that an expression is not the same as a value. For example <code>1 + 11</code>
is an  expression, whose value is  the  integer 12.  The external
representation of this integer is the character sequence <code>12</code>, i.e., this
sequence is output if the integer is printed. This sequence is another
expression whose value is the integer 12.  The process of finding the
value of an expression is done by the interpreter and is called the
<strong>evaluation</strong> of the expression.
<p>
Variables, function calls, and integer, permutation, string, function,
list, and record literals (see <a href="CHAP004.htm#SECT008">Variables</a>, <a href="CHAP004.htm#SECT010">Function Calls</a>, <a href="CHAP014.htm">Integers</a>,
<a href="CHAP040.htm">Permutations</a>,  <a href="CHAP026.htm">Strings  and  Characters</a>,  <a href="CHAP004.htm#SSEC022.1">Function</a>s,  <a href="CHAP021.htm">Lists</a>,
<a href="CHAP027.htm">Records</a>), are the simplest cases of expressions.
<p>
Expressions, for example the simple expressions mentioned above, can be
combined with the operators to form more complex expressions. Of course
those expressions can then be combined further with the operators to form
even more complex expressions. The <strong>operators</strong> fall into three classes.
<a name = "I6"></a>

The <strong>comparisons</strong> are <code>=</code>, <code>&lt;&gt;</code>, <code>&lt;</code>, <code>&lt;=</code>, <code>&gt;</code>,  <code>&gt;=</code>, and <code>in</code> (see
<a href="CHAP004.htm#SECT011">Comparisons</a> and <a href="CHAP028.htm#SECT005">Membership Test for Collections</a>).
The <strong>arithmetic operators</strong> are <code>+</code>, <code>-</code>, <code>*</code>,
<code>/</code>, <code>mod</code>, and <code>^</code> (see&nbsp;<a href="CHAP004.htm#SECT012">Arithmetic Operators</a>).
The <strong>logical operators</strong> are <code>not</code>, <code>and</code>, and <code>or</code>
(see&nbsp;<a href="CHAP020.htm#SECT003">Operations for Booleans</a>).
<p>
The following example shows a very simple expression with value 4 and a
more complex expression.
<p>
<pre>
gap&gt; 2 * 2;
4
gap&gt; 2 * 2 + 9 = Fibonacci(7) and Fibonacci(13) in Primes;
true
</pre>
<p>
For the precedence of operators, see&nbsp;<a href="CHAP004.htm#SECT011">Comparisons</a>.
<p>
<p>
<h2><a name="SECT008">4.8 Variables</a></h2>
<p><p>
<a name = "I7"></a>

<a name = "I7"></a>
<a name = "I8"></a>

A <strong>variable</strong> is a location in a <font face="Gill Sans,Helvetica,Arial">GAP</font> program that points to a value.
We say the variable is <strong>bound</strong> to this value. If a variable is evaluated
it evaluates to this value.
<p>
Initially an ordinary variable is not bound to any value. The variable
can be bound to a value by <strong>assigning</strong> this value to the variable (see
<a href="CHAP004.htm#SECT014">Assignments</a>). Because of this we sometimes say that a variable that is
not bound to any value has no assigned value. Assignment is in fact the
only way by which a variable, which is not an argument of a function, can
be bound to a value. After a variable has been bound to a  value an
assignment can also be used to bind the variable to another value.
<p>
A special class of variables is the class of <strong>arguments</strong> of functions.
They behave similarly to other variables,
except they are bound to the value of the
actual arguments upon a function call (see <a href="CHAP004.htm#SECT010">Function Calls</a>).
<p>
Each variable has a name that is also called its <strong>identifier</strong>. This is
because in a given scope an identifier identifies a unique variable (see
<a href="CHAP004.htm#SECT006">Identifiers</a>). A <strong>scope</strong> is a lexical part of a program text. There is
the <strong>global scope</strong> that encloses the entire program text, and there are
local scopes that range from the <code>function</code> keyword, denoting the
beginning of a function definition, to the corresponding <code>end</code> keyword.
A <strong>local scope</strong> introduces new variables, whose identifiers are given in
the formal argument list and the <code>local</code> declaration of the function (see
<a href="CHAP004.htm#SSEC022.1">Function</a>). Usage of an identifier in a program text refers to the
variable in the innermost scope that has this identifier as its name.
Because this mapping from identifiers to variables is done when the
program is read, not when it is executed, <font face="Gill Sans,Helvetica,Arial">GAP</font> is said to have <strong>lexical
scoping</strong>.  The following example shows how one identifier refers to
different variables at different points in the program text.
<p>
<pre>
g := 0;      # global variable g
x := function ( a, b, c )
  local  y;
  g := c;     # c refers to argument c of function x
  y := function ( y )
    local d, e, f;
    d := y;   # y refers to argument y of function y
    e := b;   # b refers to argument b of function x
    f := g;   # g refers to global variable g
    return d + e + f;
  end;
  return y( a ); # y refers to local y of function x
end;
</pre>
<p>
It is important to note that the concept of a variable in <font face="Gill Sans,Helvetica,Arial">GAP</font> is quite
different from the concept of a variable in programming languages like
PASCAL.
<p>
In those languages a variable denotes a block of memory. The
value of the variable is stored in this block. So in those languages two
variables can have the same value, but they can never have identical
values, because they denote different blocks of memory.  Note that
PASCAL has the concept of a reference argument. It seems as if such an
argument and the variable used in the actual function call have the same
value, since changing the argument's value also changes the value of the
variable used in the actual function call.  But this is not so; the
reference argument is actually a pointer to the variable used in the
actual function call, and it is the compiler that inserts enough magic to
make the pointer invisible.  In order for this to work the compiler
needs enough information to compute the amount of memory needed for each
variable in a program, which is readily available in the declarations
PASCAL requires for every variable.
<p>
In <font face="Gill Sans,Helvetica,Arial">GAP</font> on the other hand each variable just points to a value,
and different variables can share the same value.
<p>
<a name = "SSEC008.1"></a>
<li><code>Unbind( </code><var>ident</var><code> ) F</code>
<p>
deletes the identifier <var>ident</var>. If there is no other variable pointing to
the same value as <var>ident</var> was, this value will be removed by the next
garbage collection. Therefore <code>Unbind</code> can be used to get rid of unwanted
large objects.
<p>
For records and lists <code>Unbind</code> can be used to delete components or entries,
respectively (see Chapters&nbsp;<a href="CHAP027.htm">Records</a> and <a href="CHAP021.htm">Lists</a>).
<p>
<p>
<h2><a name="SECT009">4.9 More About Global Variables</a></h2>
<p><p>
The vast majority of variables in <font face="Gill Sans,Helvetica,Arial">GAP</font> are defined at the outer
level (the global scope). They are used to access functions and
other objects created either in the <font face="Gill Sans,Helvetica,Arial">GAP</font> library or in the user's
code. Certain special facilities are provided for manipulating these
variables which are not available for other types of variable (such as 
local variables or function arguments).
<p>
First, such variables may be marked <strong>read-only</strong>. In which case
attempts to change them will fail. Most of the global variables
defined in the <font face="Gill Sans,Helvetica,Arial">GAP</font> library are so marked.
<p>
<a name = "SSEC009.1"></a>
<li><code>IsReadOnlyGlobal( </code><var>name</var><code> ) F</code>
<p>
returns <code>true</code> if the global variable named by the string <var>name</var> is 
read-only and <code>false</code> otherwise (the default). 
<p>
<a name = "SSEC009.2"></a>
<li><code>MakeReadOnlyGlobal( </code><var>name</var><code> ) F</code>
<p>
marks the global variable named by the string <var>name</var> as read-only. 
<p>
A warning is given if <var>name</var> has no value bound to it or if it is
already read-only.
<p>
<a name = "SSEC009.3"></a>
<li><code>MakeReadWriteGlobal( </code><var>name</var><code> ) F</code>
<p>
marks the global variable named by the string <var>name</var> as read-write.
<p>
A warning is given if <var>name</var> is already read-write.
<p>
<pre>
gap&gt; xx := 17;
17
gap&gt; IsReadOnlyGlobal("xx");
false
gap&gt; xx := 15;
15
gap&gt; MakeReadOnlyGlobal("xx");
gap&gt; xx := 16;
Variable: 'xx' is read only
not in any function
Entering break read-eval-print loop ...
you can 'quit;' to quit to outer loop, or
you can 'return;' after making it writable to continue
brk&gt; quit;
gap&gt; IsReadOnlyGlobal("xx");
true
gap&gt; MakeReadWriteGlobal("xx");
gap&gt; xx := 16;
16
gap&gt; IsReadOnlyGlobal("xx");
false
</pre>
<p>
A group of functions are also supplied for accessing and altering the
values assigned to global variables. Use of these functions differs
from the use of assignment, <code>Unbind</code> and <code>IsBound</code> statements, in two
ways.
First, these functions always affect global variables, even if 
local variables of the same names exist.
Second, the variable names are passed as strings,
rather than being written directly into the statements. 
<p>
<a name = "SSEC009.4"></a>
<li><code>ValueGlobal( </code><var>name</var><code> ) F</code>
<p>
returns the value currently bound to the global variable named by the 
string <var>name</var>. An error is raised if no value is currently bound.
<p>
<a name = "SSEC009.5"></a>
<li><code>IsBoundGlobal( </code><var>name</var><code> ) F</code>
<p>
returns <code>true</code> if a value currently bound
to the global variable named by the string <var>name</var> and <code>false</code> otherwise.
<p>
<a name = "SSEC009.6"></a>
<li><code>UnbindGlobal( </code><var>name</var><code> ) F</code>
<p>
removes any value currently bound
to the global variable named by the string <var>name</var>. Nothing is returned.
<p>
A warning is given if <var>name</var> was not bound. The global variable named
by <var>name</var> must be writable, otherwise an error is raised.
<p>
<a name = "SSEC009.7"></a>
<li><code>BindGlobal( </code><var>name</var><code>, </code><var>val</var><code> ) F</code>
<p>
sets the global variable named by the
string <var>name</var> to the value <var>val</var>, provided it is writable, and makes
it read-only. If <var>name</var> already has a value, a warning message is
printed.  
<p>
This is intended to be the normal way to create and set ``official''
global variables (such as Operations and Categories).  
<p>
Caution should be exercised in using these functions, especially
<code>BindGlobal</code> and <code>UnbindGlobal</code> as unexpected changes in global
variables can be very confusing for the user.
<p>
<pre>
gap&gt; xx := 16;
16
gap&gt; IsReadOnlyGlobal("xx");
false
gap&gt; ValueGlobal("xx");
16
gap&gt; IsBoundGlobal("xx");
true
gap&gt; BindGlobal("xx",17);
#W BIND_GLOBAL: variable `xx' already has a value
gap&gt; xx;
17
gap&gt; IsReadOnlyGlobal("xx");
true
</pre>
<p>
Finally, there are a group of functions dealing with the <strong>global
namespace</strong>.
<p>
<a name = "SSEC009.8"></a>
<li><code>NamesGVars() F</code>
<p>
This function returns an immutable (see&nbsp;<a href="CHAP012.htm#SECT006">Mutability and Copyability</a>) sorted
(see&nbsp;<a href="CHAP021.htm#SECT019">Sorted Lists and Sets</a>) list of all the global
variable names known to the system.  This includes names of variables
which were bound but have now been unbound and some other names which
have never been bound but have become known to the system by various
routes.
<p>
<a name = "SSEC009.9"></a>
<li><code>NamesSystemGVars() F</code>
<p>
This function returns an immutable sorted list of all the global
variable names created by the <font face="Gill Sans,Helvetica,Arial">GAP</font> library when <font face="Gill Sans,Helvetica,Arial">GAP</font> was started.
<p>
<a name = "SSEC009.10"></a>
<li><code>NamesUserGVars() F</code>
<p>
This function returns an immutable sorted list of the global variable
names created since the library was read, to which a value is
currently bound.
<p>
<a name = "SSEC009.11"></a>
<li><code>TemporaryGlobalVarName( [</code><var>prefix</var><code>] ) F</code>
<p>
returns a string  that can be used
as the  name  of a global  variable  that is not bound   at the time when
<code>TemporaryGlobalVarName()</code>  is called.    The optional  argument <var>prefix</var> can
specify a string with which the name of the global variable starts.
<p>
<p>
<h2><a name="SECT010">4.10 Function Calls</a></h2>
<p><p>
<a name = "SSEC010.1"></a>
<li><code></code><var>function-var</var><code>()</code>
<a name = "SSEC010.1"></a>
<li><code></code><var>function-var</var><code>( </code><var>arg-expr</var><code>[, </code><var>arg-expr</var><code>, ...] )</code>
<p>
The function call has the effect of calling the function <var>function-var</var>.
The precise semantics are as follows.
<p>
First <font face="Gill Sans,Helvetica,Arial">GAP</font> evaluates the <var>function-var</var>.
Usually <var>function-var</var> is a variable,
and <font face="Gill Sans,Helvetica,Arial">GAP</font> does nothing more than taking the value of this variable.
It is allowed though that <var>function-var</var> is a more complex expression,
such as a reference to an element of a list (see Chapter&nbsp;<a href="CHAP021.htm">Lists</a>)
<code></code><var>list-var</var><code>[</code><var>int-expr</var><code>]</code>,
or to a component of a record (see Chapter&nbsp;<a href="CHAP027.htm">Records</a>) <code></code><var>record-var</var><code>.</code><var>ident</var><code></code>.
In any case <font face="Gill Sans,Helvetica,Arial">GAP</font> tests whether the value is a function.
If it is not, <font face="Gill Sans,Helvetica,Arial">GAP</font> signals an error.
<p>
<a name = "I9"></a>

<a name = "I10"></a>

Next <font face="Gill Sans,Helvetica,Arial">GAP</font> checks that the number of actual arguments <var>arg-expr</var>s agrees
with the number of <strong>formal arguments</strong> as given in the function definition.
If they do not agree <font face="Gill Sans,Helvetica,Arial">GAP</font> signals an error. An exception is the case
when there is exactly one formal argument with the name <code>arg</code>, in which
case any number of actual arguments is allowed (see&nbsp;<a href="CHAP004.htm#SSEC022.1">function</a> for
examples).
<p>
Now <font face="Gill Sans,Helvetica,Arial">GAP</font> allocates for each formal argument and for each <strong>formal local</strong>
(that is, the identifiers in the <code>local</code> declaration) a new variable.
Remember that a variable is a location in a <font face="Gill Sans,Helvetica,Arial">GAP</font> program
that points to a value. Thus for  each formal argument and for each
formal local such a location is allocated.
<p>
Next the arguments <var>arg-expr</var>s are evaluated, and the values are assigned
to the newly created variables corresponding to the formal arguments. Of
course the first value is assigned to the new variable corresponding to
the first formal argument, the second  value  is assigned to the new
variable corresponding  to  the second  formal argument, and  so on.
However, <font face="Gill Sans,Helvetica,Arial">GAP</font> does not make any guarantee about the order in which the
arguments are evaluated. They might be evaluated left to right, right to
left, or in any other order, but each argument is evaluated once. An
exception again occurs if the function has only one formal argument with
the name <code>arg</code>. In this case the values of all the actual arguments are
stored in  a list and this  list is assigned to the  new variable
corresponding to the formal argument <code>arg</code>.
<p>
The new variables corresponding to the formal locals are initially not
bound to any  value.  So trying  to evaluate those  variables before
something has been assigned to them will signal an error.
<p>
Now the body of the function, which is a statement, is executed. If the
identifier of one of the formal arguments or formal locals appears in the
body of the function it refers to the new variable that was allocated for
this formal argument or formal local, and evaluates to the value of this
variable.
<p>
If during the execution of the body of the function a <code>return</code> statement
with an expression (see <a href="CHAP004.htm#SECT023">Return</a>) is executed, execution of the body is
terminated and the value of the function call is the value of the
expression of the <code>return</code>. If during the execution of the body a
<code>return</code> statement without an expression is executed, execution of the
body is terminated and the function call does not produce a value, in
which case we call this call a procedure call (see <a href="CHAP004.htm#SECT015">Procedure Calls</a>).
If the execution of the body completes without execution of a <code>return</code>
statement, the function call again produces no value, and again we talk
about a procedure call.
<p>
<pre>
gap&gt; Fibonacci( 11 );
89
</pre>
<p>
The above example shows a call to the function <code>Fibonacci</code> with actual
argument <code>11</code>, the following one shows a call to the operation
<code>RightCosets</code> where the second actual argument is another function call.
<p>
<pre>
gap&gt; RightCosets( G, Intersection( U, V ) );;
</pre>
<p>
<a name = "SSEC010.2"></a>
<li><code></code><var>function-var</var><code>( </code><var>arg-expr</var><code>[, </code><var>arg-expr</var><code>, ...][ : [ </code><var>option-expr</var><code> [,</code><var>option-expr</var><code>, ....]]])</code>
<p>
As well as passing arguments to a function, providing the mathematical 
input to its calculation, it is sometimes useful to supply ``hints''
suggesting to <font face="Gill Sans,Helvetica,Arial">GAP</font> how the desired result may be computed more
quickly, or specifying a level of tolerance for random errors in a
Monte Carlo algorithm.
<p>
Such hints may be supplied to a function-call <strong>and to all subsidiary
functions called from that call</strong> using the options mechanism. Options
are separated from the actual arguments by a colon <code>:</code> and have much
the same syntax as the components of a record expression. The one
exception to this is that a component name may appear without a value,
in which case the value <code>true</code> is silently inserted.
<p>
The following example shows a call to <code>Size</code> passing the options <code>hard</code>
(with the value <code>true</code>) and <code>tcselection</code> (with the string ``external''
as value).
<p>
<pre>
gap&gt; Size( fpgrp : hard, tcselection := "external" );
</pre>
<p>
Options supplied with function calls in this way are passed down using 
the global options stack described in chapter <a href="../ref/CHAP008.htm">Options Stack</a>, so that
the call above is exactly equivalent to
<p>
<pre>
gap&gt; PushOptions( rec( hard := true, tcselection := "external") );
gap&gt; Size( fpgrp );
gap&gt; PopOptions( );
</pre>
<p>
<strong>Note</strong> that any option may be passed with any function, whether or not 
it has any actual meaning for that function, or any function called by 
it. The system provides no safeguard against misspelled option names.
<p>
<p>
<h2><a name="SECT011">4.11 Comparisons</a></h2>
<p><p>
<a name = "SSEC011.1"></a>
<li><code></code><var>left-expr</var><code> = </code><var>right-expr</var><code></code>
<a name = "SSEC011.1"></a>
<li><code></code><var>left-expr</var><code> &lt;&gt; </code><var>right-expr</var><code></code>
<p>
The operator <code>=</code> tests for equality of its two operands and evaluates to
<code>true</code> if they are equal and to <code>false</code> otherwise. Likewise <code>&lt;&gt;</code> tests
for inequality of its two operands. Note that any two objects can be
compared, i.e., <code>=</code> and <code>&lt;&gt;</code> will never signal an error. For each type
of objects the definition of equality is given in the respective chapter.
Objects in different families (see&nbsp;<a href="CHAP013.htm#SECT001">Families</a>) are never equal,
i.e., <code>=</code> evaluates in this case to <code>false</code>, and <code>&lt;&gt;</code> evaluates to <code>true</code>.
<p>
<a name = "SSEC011.2"></a>
<li><code></code><var>left-expr</var><code> &lt; </code><var>right-expr</var><code></code>
<a name = "SSEC011.2"></a>
<li><code></code><var>left-expr</var><code> &gt;  </code><var>right-expr</var><code></code>
<a name = "SSEC011.2"></a>
<li><code></code><var>left-expr</var><code> &lt;= </code><var>right-expr</var><code></code>
<a name = "SSEC011.2"></a>
<li><code></code><var>left-expr</var><code> &gt;= </code><var>right-expr</var><code></code>
<p>
<code>&lt;</code> denotes less than, <code>&lt;=</code> less than or equal, <code>&gt;</code> greater than, and
<code>&gt;=</code> greater than or equal of its two operands.
For each kind of objects the definition of the ordering is given in the
respective chapter.
<p>
Only for the following kinds of objects, an ordering via <code>&lt;</code> of objects
in <strong>different</strong> families (see&nbsp;<a href="CHAP013.htm#SECT001">Families</a>) is supported.
Rationals (see&nbsp;<a href="CHAP016.htm#SSEC001.1">IsRat</a>) are smallest,
next are cyclotomics (see&nbsp;<a href="CHAP018.htm#SSEC001.3">IsCyclotomic</a>),
followed by finite field elements (see&nbsp;<a href="CHAP057.htm#SSEC001.1">IsFFE</a>);
finite field elements in different characteristics are compared
via their characteristics,
next are permutations (see&nbsp;<a href="CHAP040.htm">IsPerm</a>),
followed by the boolean values <code>true</code>, <code>false</code>, and <code>fail</code>
(see&nbsp;<a href="CHAP020.htm">IsBool</a>),
characters (such as <code>'a'</code>, see&nbsp;<a href="CHAP026.htm">IsChar</a>),
and lists (see&nbsp;<a href="CHAP021.htm#SSEC001.1">IsList</a>) are largest;
note that two lists can be compared with <code>&lt;</code> if and only if their
elements are again objects that can be compared with <code>&lt;</code>.
<p>
For other objects, <font face="Gill Sans,Helvetica,Arial">GAP</font> does <strong>not</strong> provide an ordering via <code>&lt;</code>.
The reason for this is that a total ordering of all <font face="Gill Sans,Helvetica,Arial">GAP</font> objects
would be hard to maintain when new kinds of objects are introduced,
and such a total ordering is hardly used in its full generality.
<p>
However, for objects in the filters listed above, the ordering via <code>&lt;</code>
has turned out to be useful.
For example, one can form <strong>sorted lists</strong> containing integers and nested
lists of integers, and then search in them using <code>PositionSorted</code>
(see&nbsp;<a href="CHAP021.htm#SECT016">Finding Positions in Lists</a>).
<p>
Of course it would in principle be possible to define an ordering
via <code>&lt;</code> also for certain other objects,
by installing appropriate methods for the operation <code>\&lt;</code>.
But this may lead to problems at least as soon as one loads <font face="Gill Sans,Helvetica,Arial">GAP</font> code
in which the same is done, under the assumption that one is completely
free to define an ordering via <code>&lt;</code> for other objects than the ones
for which the ``official'' <font face="Gill Sans,Helvetica,Arial">GAP</font> provides already an ordering via <code>&lt;</code>.
<p>
Comparison operators, including the operator <code>in</code>
(see&nbsp;<a href="CHAP021.htm#SECT008">Membership Test for Lists</a>),
are not associative,
Hence it is not allowed to write <code></code><var>a</var><code> = </code><var>b</var><code> &lt;&gt; </code><var>c</var><code> = </code><var>d</var><code></code>,
you must use <code>(</code><var>a</var><code> = </code><var>b</var><code>) &lt;&gt; (</code><var>c</var><code> = </code><var>d</var><code>)</code> instead.
The comparison operators have  higher precedence than the logical operators
<a name = "I11"></a>

(see&nbsp;<a href="CHAP020.htm#SECT003">Operations for Booleans</a>), but lower precedence than the arithmetic
operators (see&nbsp;<a href="CHAP004.htm#SECT012">Arithmetic Operators</a>).
Thus, for instance, <code></code><var>a</var><code> * </code><var>b</var><code> = </code><var>c</var><code> and </code><var>d</var><code></code> is interpreted as
<code>((</code><var>a</var><code> * </code><var>b</var><code>) = </code><var>c</var><code>) and </code><var>d</var><code>)</code>.
<p>
The following example shows a comparison where the left operand is an
expression.
<p>
<pre>
gap&gt; 2 * 2 + 9 = Fibonacci(7);
true
</pre>
<p>
For the underlying operations of the operators introduced above,
see&nbsp;<a href="CHAP030.htm#SECT011">Comparison Operations for Elements</a>.
<p>
<p>
<h2><a name="SECT012">4.12 Arithmetic Operators</a></h2>
<p><p>
<a name = "I12"></a>

<a name = "I12"></a>
<a name = "I13"></a>

<a name = "I12"></a>
<a name = "I13"></a>
<a name = "I14"></a>

<a name = "I15"></a>

<a name = "I15"></a>
<a name = "I16"></a>

<a name = "I15"></a>
<a name = "I16"></a>
<a name = "I17"></a>

<a name = "I15"></a>
<a name = "I16"></a>
<a name = "I17"></a>
<a name = "I18"></a>

<a name = "I15"></a>
<a name = "I16"></a>
<a name = "I17"></a>
<a name = "I18"></a>
<a name = "I19"></a>

<a name = "I20"></a>

<a name = "I21"></a>

<a name = "I21"></a>
<a name = "I22"></a>

<a name = "SSEC012.1"></a>
<li><code>+ </code><var>right-expr</var><code></code>
<a name = "SSEC012.1"></a>
<li><code>- </code><var>right-expr</var><code></code>
<a name = "SSEC012.1"></a>
<li><code></code><var>left-expr</var><code> + </code><var>right-expr</var><code></code>
<a name = "SSEC012.1"></a>
<li><code></code><var>left-expr</var><code> - </code><var>right-expr</var><code></code>
<a name = "SSEC012.1"></a>
<li><code></code><var>left-expr</var><code> * </code><var>right-expr</var><code></code>
<a name = "SSEC012.1"></a>
<li><code></code><var>left-expr</var><code> / </code><var>right-expr</var><code></code>
<a name = "SSEC012.1"></a>
<li><code></code><var>left-expr</var><code> mod </code><var>right-expr</var><code></code>
<a name = "SSEC012.1"></a>
<li><code></code><var>left-expr</var><code> ^ </code><var>right-expr</var><code></code>
<p>
The arithmetic operators are <code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>, <code>mod</code>, and <code>^</code>.
The meanings (semantics) of those operators generally depend on the types
of the operands involved, and, except for <code>mod</code>, they are defined in the 
various chapters describing the types. However basically the meanings are
as follows.
<p>
<code></code><var>a</var><code> + </code><var>b</var><code></code> denotes the addition of additive elements <var>a</var> and <var>b</var>.
<p>
<code></code><var>a</var><code> - </code><var>b</var><code></code> denotes the addition of <var>a</var> and the additive inverse of <var>b</var>.
<p>
<code></code><var>a</var><code> * </code><var>b</var><code></code> denotes the multiplication of multiplicative elements <var>a</var> and
<var>b</var>.
<p>
<code></code><var>a</var><code> / </code><var>b</var><code></code> denotes the multiplication of <var>a</var> with the multiplicative
inverse of <var>b</var>.
<p>
<a name = "I23"></a>

<code></code><var>a</var><code> mod </code><var>b</var><code></code>, for integer or rational left operand <var>a</var> and for non-zero
integer right operand <var>b</var>, is defined as follows.
If <var>a</var> and <var>b</var> are both integers, <code></code><var>a</var><code> mod </code><var>b</var><code></code> is the integer <var>r</var> in the
integer range <code>0 .. |</code><var>b</var><code>| - 1</code> satisfying <code></code><var>a</var><code> = </code><var>r</var><code> + </code><var>b</var><code></code><var>q</var><code></code>,
for some integer <var>q</var> (where the operations occurring have their usual meaning
over the integers, of course).
<p>
<a name = "I24"></a>

<a name = "I24"></a>
<a name = "I25"></a>

<a name = "I26"></a>

<a name = "I26"></a>
<a name = "I27"></a>

If <var>a</var> is a rational number and <var>b</var> is a non-zero integer, and <code></code><var>a</var><code> = </code><var>m</var><code>
/ </code><var>n</var><code></code> where <var>m</var> and <var>n</var> are coprime integers  with  <var>n</var>  positive,  then
<code></code><var>a</var><code> mod </code><var>b</var><code></code> is the integer <var>r</var> in the integer range <code>0 ..  |</code><var>b</var><code>|  -  1</code>
such that <var>m</var> is congruent to <code></code><var>r</var><code></code><var>n</var><code></code> modulo <var>b</var>, and <var>r</var> is called the
``modular remainder'' of <var>a</var> modulo <var>b</var>. Also,  <code>1  /  </code><var>n</var><code>  mod  </code><var>b</var><code></code>  is
called the ``modular inverse'' of <var>n</var> modulo <var>b</var>. (A pair of integers  is
said to be <strong>coprime</strong> (or <strong>relatively prime</strong>) if their gcd is 1.)
<p>
With the above definition, <code>4 / 6 mod 32</code> equals <code>2 / 3 mod 32</code> and hence
exists (and is equal to 22), despite the  fact  that  6  has  no  inverse
modulo 32.
<p>
<strong>Note.</strong>
For rational <var>a</var>, <code></code><var>a</var><code> mod </code><var>b</var><code></code>  could have been defined to be the
non-negative rational <var>c</var> less than <code>|</code><var>b</var><code>|</code> such that <code></code><var>a</var><code> - </code><var>c</var><code></code> is a
multiple of <var>b</var>. However this definition is seldom useful and <strong>not</strong> the
one chosen for <font face="Gill Sans,Helvetica,Arial">GAP</font>.
<p>
<code>+</code> and <code>-</code> can also be used as unary operations.
The unary <code>+</code> is ignored. The unary <code>-</code> returns the additive inverse of
its operand; over the integers it is equivalent to multiplication by <code>-1</code>.
<p>
<code>^</code> denotes powering of a multiplicative element if the right operand  is
an integer, and is also used to denote the action of a group element on a
point of a set if the right operand is a group element.
<p>
<a name = "I28"></a>

The <strong>precedence</strong> of those operators is as follows. The powering operator
<code>^</code> has the highest precedence, followed by the unary operators <code>+</code> and
<code>-</code>, which are followed by the multiplicative operators <code>*</code>, <code>/</code>, and
<code>mod</code>, and the additive binary operators <code>+</code> and <code>-</code> have the lowest
precedence.  That means that the expression <code>-2 ^ -2 * 3 + 1</code> is
interpreted as <code>(-(2 ^ (-2)) * 3) + 1</code>. If in doubt use parentheses
to clarify your intention.
<p>
<a name = "I29"></a>

The <strong>associativity</strong> of the arithmetic operators is as follows.
<code>^</code> is not associative, i.e., it is illegal to write <code>2^3^4</code>,
use parentheses to clarify whether you mean <code>(2^3)^4</code> or <code>2^(3^4)</code>.
The unary operators <code>+</code> and <code>-</code> are right associative,
because they are written to the left of their operands.
<code>*</code>, <code>/</code>, <code>mod</code>, <code>+</code>, and <code>-</code> are all left associative,
i.e., <code>1-2-3</code> is interpreted as <code>(1-2)-3</code> not as <code>1-(2-3)</code>.
Again, if in doubt use parentheses to clarify your intentions.
<p>
The arithmetic operators have higher precedence than the comparison
operators (see&nbsp;<a href="CHAP004.htm#SECT011">Comparisons</a> and&nbsp;<a href="CHAP028.htm#SECT005">Membership Test for Collections</a>)
and the logical operators (see
<a href="CHAP020.htm#SECT003">Operations for Booleans</a>). Thus, for example, <code></code><var>a</var><code> * </code><var>b</var><code> = </code><var>c</var><code> and
</code><var>d</var><code></code> is interpreted, <code>((</code><var>a</var><code> * </code><var>b</var><code>) = </code><var>c</var><code>) and </code><var>d</var><code></code>.
<p>
<pre>
gap&gt; 2 * 2 + 9;  # a very simple arithmetic expression
13
</pre>
<p>
For other arithmetic operations, and for the underlying operations of
the operators introduced above, see&nbsp;<a href="CHAP030.htm#SECT012">Arithmetic Operations for Elements</a>.
<p>
<p>
<h2><a name="SECT013">4.13 Statements</a></h2>
<p><p>
<a name = "I30"></a>

Assignments (see <a href="CHAP004.htm#SECT014">Assignments</a>), Procedure calls (see <a href="CHAP004.htm#SECT015">Procedure Calls</a>),
<code>if</code> statements (see <a href="CHAP004.htm#SECT016">If</a>), <code>while</code> (see <a href="CHAP004.htm#SECT017">While</a>), <code>repeat</code>  (see
<a href="CHAP004.htm#SECT018">Repeat</a>) and <code>for</code> loops (see <a href="CHAP004.htm#SECT019">For</a>), and the <code>return</code> statement (see
<a href="CHAP004.htm#SECT023">Return</a>) are called <strong>statements</strong>. They can be entered interactively or be
part of a function definition. Every statement must be terminated by a
semicolon.
<p>
Statements, unlike expressions, have no value. They are executed only to
produce an effect. For example an assignment has the effect of assigning
a  value to a variable, a <code>for</code> loop  has the effect of executing a
statement sequence for all elements in a list and so on. We will talk
about <strong>evaluation</strong> of expressions but about <strong>execution</strong> of statements to
emphasize this difference.
<p>
Using expressions as statements is treated as syntax error.
<p>
<pre>
gap&gt; i := 7;;
gap&gt; if i &lt;&gt; 0 then k = 16/i; fi;
Syntax error: := expected
if i &lt;&gt; 0 then k = 16/i; fi;
                 ^
gap&gt; 
</pre>
<p>
As you can see from the example this warning does in particular address
those users who are used to languages where <code>=</code> instead of <code>:=</code> denotes
assignment.
<p>
Empty statements are permitted and have no effect.
<p>
A sequence of one or more statements is a <strong>statement sequence</strong>, and may
occur everywhere instead of a single statement. There is nothing like
PASCAL's BEGIN-END, instead each construct is terminated by a keyword.
The simplest statement sequence is a single semicolon, which can be
used as an empty statement sequence. In fact an empty statement
sequence as in <code>for i in [1..2] do od</code> is also permitted and is
silently translated into the sequence containing just a semicolon.
<p>
<p>
<h2><a name="SECT014">4.14 Assignments</a></h2>
<p><p>
<a name = "SSEC014.1"></a>
<li><code></code><var>var</var><code> := </code><var>expr</var><code>;</code>
<p>
The <strong>assignment</strong> has the effect of assigning the value of the expressions
<var>expr</var> to the variable <var>var</var>.
<p>
The variable <var>var</var> may be an ordinary variable (see <a href="CHAP004.htm#SECT008">Variables</a>), a list
element selection <code></code><var>list-var</var><code>[</code><var>int-expr</var><code>]</code> (see <a href="CHAP021.htm#SECT004">List Assignment</a>) or a
record component  selection  <code></code><var>record-var</var><code>.</code><var>ident</var><code></code>  (see  <a href="CHAP027.htm#SECT002">Record Assignment</a>). Since a list element or a record component may itself be a
list or a record the left hand side of an assignment may be arbitrarily
complex.
<p>
Note that variables do not have a type. Thus any value may be assigned
to any variable.  For example a variable with an integer value may be
assigned a permutation or a list or anything else.
<p>
<pre>
gap&gt; data:= rec( numbers:= [ 1, 2, 3 ] );
rec( numbers := [ 1, 2, 3 ] )
gap&gt; data.string:= "string";; data;
rec( numbers := [ 1, 2, 3 ], string := "string" )
gap&gt; data.numbers[2]:= 4;; data;
rec( numbers := [ 1, 4, 3 ], string := "string" )
</pre>
<p>
If the expression <var>expr</var> is a function call then this function must
return a value.  If the function does not return a value an error is
signalled and you enter a break loop (see <a href="CHAP006.htm#SECT004">Break Loops</a>).  As usual you
can leave the break  loop  with <code>quit;</code>.  If you enter  <code>return
</code><var>return-expr</var><code>;</code> the value of the expression <var>return-expr</var> is assigned to
the variable, and execution continues after the assignment.
<p>
<pre>
gap&gt; f1:= function( x ) Print( "value: ", x, "\n" ); end;;
gap&gt; f2:= function( x ) return f1( x ); end;;
gap&gt; f2( 4 );
value: 4
Function Calls: &lt;func&gt; must return a value at
return f1( x );
 called from
&lt;function&gt;( &lt;arguments&gt; ) called from read-eval-loop
Entering break read-eval-print loop ...
you can 'quit;' to quit to outer loop, or
you can supply one by 'return &lt;value&gt;;' to continue
brk&gt; return "hello";
"hello"
</pre>
<p>
In the above example, the function <code>f2</code> calls <code>f1</code> with argument <code>4</code>,
and since <code>f1</code> does not return a value (but only prints a line ``<code>value:
</code><var>x</var><code></code>''), the <code>return</code> statement of <code>f2</code>
cannot be executed.
The error message says that it is possible to return an appropriate value,
and the returned string <code>"hello"</code> is used by <code>f2</code> instead of the missing
return value of <code>f1</code>.
<p>
<p>
<h2><a name="SECT015">4.15 Procedure Calls</a></h2>
<p><p>
<a name = "SSEC015.1"></a>
<li><code></code><var>procedure-var</var><code>();</code>
<a name = "SSEC015.1"></a>
<li><code></code><var>procedure-var</var><code>( </code><var>arg-expr</var><code> [,</code><var>arg-expr</var><code>, ...] );</code>
<p>
The <strong>procedure call</strong> has  the  effect  of calling  the procedure
<var>procedure-var</var>.  A procedure call is done exactly like a function call
(see <a href="CHAP004.htm#SECT010">Function Calls</a>). The distinction between functions and procedures
is only for the sake of the discussion, <font face="Gill Sans,Helvetica,Arial">GAP</font> does not distinguish
between them.
So we state the following conventions.
<p>
A <strong>function</strong> does return a value but does not produce a side effect. As
a convention the name of a function is a noun, denoting what the function
returns, e.g., <code>Length</code>, <code>Concatenation</code> and <code>Order</code>.
<p>
A <strong>procedure</strong> is a function that does not return a value but produces
some  effect. Procedures are called  only for  this effect. As  a
convention the name of a procedure is a verb, denoting what the procedure
does, e.g., <code>Print</code>, <code>Append</code> and <code>Sort</code>.
<p>
<pre>
gap&gt; Read( "myfile.g" );   # a call to the procedure Read
gap&gt; l := [ 1, 2 ];;
gap&gt; Append( l, [3,4,5] );  # a call to the procedure Append
</pre>
<p>
There are a few exceptions of <font face="Gill Sans,Helvetica,Arial">GAP</font> functions that do both return
a value and produce some effect.
An example is <code>Sortex</code> which sorts a list and returns the corresponding
permutation of the entries (see&nbsp;<a href="CHAP021.htm#SSEC018.3">Sortex</a>).
<p>
<p>
<h2><a name="SECT016">4.16 If</a></h2>
<p><p>
<a name = "I31"></a>

<a name = "I31"></a>
<a name = "I32"></a>

<a name = "I31"></a>
<a name = "I32"></a>
<a name = "I33"></a>

<a name = "I31"></a>
<a name = "I32"></a>
<a name = "I33"></a>
<a name = "I34"></a>

<a name = "SSEC016.1"></a>
<li><code>if </code><var>bool-expr1</var><code> then </code><var>statements1</var><code> { elif </code><var>bool-expr2</var><code> then </code><var>statements2</var><code> }[ else </code><var>statements3</var><code> ] fi;</code>
<p>
The <code>if</code> statement allows one to execute statements depending on the
value of some boolean expression. The execution is done as follows.
<p>
First the expression <var>bool-expr1</var> following the <code>if</code> is evaluated. If it
evaluates to <code>true</code> the statement sequence <var>statements1</var> after the first
<code>then</code> is executed, and the execution of the <code>if</code> statement is complete.
<p>
Otherwise the expressions <var>bool-expr2</var> following the <code>elif</code> are evaluated
in turn. There may be any number of <code>elif</code> parts, possibly none at all.
As soon as an expression evaluates to <code>true</code> the corresponding statement
sequence <var>statements2</var> is executed and execution of the <code>if</code> statement is
complete.
<p>
If the <code>if</code> expression and all, if any, <code>elif</code> expressions evaluate to
<code>false</code> and there is an <code>else</code> part, which is optional, its statement
sequence <var>statements3</var> is executed  and the  execution of  the <code>if</code>
statement is complete. If there is no <code>else</code> part the <code>if</code> statement is
complete without executing any statement sequence.
<p>
Since the <code>if</code> statement is terminated by the <code>fi</code> keyword there is no
question where an <code>else</code> part belongs,
i.e., <font face="Gill Sans,Helvetica,Arial">GAP</font> has no ``dangling else''.
In
<p>
<code>if </code><var>expr1</var><code> then if </code><var>expr2</var><code> then </code><var>stats1</var><code> else </code><var>stats2</var><code> fi; fi;</code>
<p>
the <code>else</code> part belongs to the second  <code>if</code> statement, whereas in
<p>
<code>if </code><var>expr1</var><code> then if </code><var>expr2</var><code> then </code><var>stats1</var><code> fi; else </code><var>stats2</var><code> fi;</code>
<p>
the <code>else</code> part belongs to the first <code>if</code> statement.
<p>
Since an <code>if</code> statement is not an expression it is not possible to write
<p>
<pre>
abs := if x &gt; 0 then x; else -x; fi;
</pre>
<p>
which would, even if legal syntax, be  meaningless, since the <code>if</code>
statement does not produce a value that could be assigned to <code>abs</code>.
<p>
If one of the expressions <var>bool-expr1</var>, <var>bool-expr2</var> is evaluated
and its value is neither <code>true</code> nor <code>false</code> an error is signalled
and a break loop (see <a href="CHAP006.htm#SECT004">Break Loops</a>) is entered. As usual you
can leave  the break loop with <code>quit;</code>.  If you enter <code>return true;</code>,
execution of the <code>if</code> statement continues as if the expression whose
evaluation  failed had evaluated to <code>true</code>.   Likewise, if you enter
<code>return false;</code>, execution of the <code>if</code> statement continues as if the
expression whose evaluation failed had evaluated to <code>false</code>.
<p>
<pre>
gap&gt; i := 10;;
gap&gt; if 0 &lt; i then
&gt;    s := 1;
&gt;  elif i &lt; 0 then
&gt;    s := -1;
&gt;  else
&gt;    s := 0;
&gt;  fi;
gap&gt; s;  # the sign of i
1
</pre>
<p>
<p>
<h2><a name="SECT017">4.17 While</a></h2>
<p><p>
<a name = "I35"></a>

<a name = "SSEC017.1"></a>
<li><code>while </code><var>bool-expr</var><code> do </code><var>statements</var><code> od;</code>
<p>
The <code>while</code> loop executes the statement sequence <var>statements</var> while the
condition <var>bool-expr</var> evaluates to <code>true</code>.
<p>
First <var>bool-expr</var> is evaluated. If it evaluates to <code>false</code> execution of
the <code>while</code> loop terminates and the statement immediately following the
<code>while</code> loop is executed next. Otherwise if it evaluates to <code>true</code> the
<var>statements</var> are executed and the whole process begins again.
<p>
The difference between the <code>while</code>  loop and the <code>repeat until</code> loop
(see <a href="CHAP004.htm#SECT018">Repeat</a>) is that the <var>statements</var> in the <code>repeat until</code> loop are
executed at least once, while the <var>statements</var> in the <code>while</code> loop are
not executed at all if <var>bool-expr</var> is <code>false</code> at the first iteration.
<p>
If <var>bool-expr</var> does not evaluate to <code>true</code> or <code>false</code> an error is
signalled and a break loop (see <a href="CHAP006.htm#SECT004">Break Loops</a>) is entered. As usual you
can leave the break loop with <code>quit;</code>.  If you enter <code>return false;</code>,
execution continues with the next statement immediately following the
<code>while</code> loop.  If you enter <code>return true;</code>, execution continues at
<var>statements</var>, after which the next evaluation of <var>bool-expr</var> may cause
another error.
<p>
The following example shows a <code>while</code> loop that sums up the squares
1<sup>2</sup>, 2<sup>2</sup>, &#8230; until the sum exceeds 200.
<p>
<pre>
gap&gt; i := 0;; s := 0;;
gap&gt; while s &lt;= 200 do
&gt;    i := i + 1; s := s + i^2;
&gt;  od;
gap&gt; s;
204
</pre>
<p>
A <code>while</code> loop may be left prematurely using <code>break</code>, see <a href="CHAP004.htm#SECT020">Break</a>.
<p>
<p>
<h2><a name="SECT018">4.18 Repeat</a></h2>
<p><p>
<a name = "I36"></a>

<a name = "I36"></a>
<a name = "I37"></a>

<a name = "SSEC018.1"></a>
<li><code>repeat </code><var>statements</var><code> until </code><var>bool-expr</var><code>;</code>
<p>
The <code>repeat</code> loop executes the statement sequence <var>statements</var> until the
condition <var>bool-expr</var> evaluates to <code>true</code>.
<p>
First <var>statements</var> are executed.  Then <var>bool-expr</var> is evaluated. If it
evaluates  to <code>true</code> the <code>repeat</code>  loop terminates and the statement
immediately following the <code>repeat</code> loop is executed next. Otherwise if
it evaluates to <code>false</code> the whole process begins again with the execution
of the <var>statements</var>.
<p>
The difference between the <code>while</code> loop (see <a href="CHAP004.htm#SECT017">While</a>) and the <code>repeat
until</code> loop is that the <var>statements</var> in the <code>repeat until</code> loop are
executed at least once, while the <var>statements</var> in the <code>while</code> loop are
not executed at all if <var>bool-expr</var> is <code>false</code> at the first iteration.
<p>
If <var>bool-expr</var> does not evaluate to <code>true</code>  or <code>false</code> an error is
signalled and a break loop (see <a href="CHAP006.htm#SECT004">Break Loops</a>) is entered. As usual you
can leave the break loop with <code>quit;</code>.  If you enter <code>return true;</code>,
execution continues with the next statement  immediately following the
<code>repeat</code> loop.  If you enter <code>return  false;</code>, execution continues at
<var>statements</var>, after which the next evaluation of <var>bool-expr</var> may cause
another error.
<p>
The <code>repeat</code> loop in the following example has the same purpose as the
<code>while</code> loop in the preceding example, namely to sum up the squares
1<sup>2</sup>, 2<sup>2</sup>, &#8230; until the sum exceeds 200.
<p>
<pre>
gap&gt; i := 0;; s := 0;;
gap&gt; repeat
&gt;    i := i + 1; s := s + i^2;
&gt;  until s &gt; 200;
gap&gt; s;
204
</pre>
<p>
A <code>repeat</code> loop may be left prematurely using <code>break</code>, see <a href="CHAP004.htm#SECT020">Break</a>.
<p>
<p>
<h2><a name="SECT019">4.19 For</a></h2>
<p><p>
<a name = "I38"></a>

<a name = "I38"></a>
<a name = "I39"></a>

<a name = "I38"></a>
<a name = "I39"></a>
<a name = "I40"></a>

<a name = "SSEC019.1"></a>
<li><code>for </code><var>simple-var</var><code> in </code><var>list-expr</var><code> do </code><var>statements</var><code> od;</code>
<p>
The <code>for</code> loop executes  the statement sequence <var>statements</var> for every
element of the list <var>list-expr</var>.
<p>
The statement sequence <var>statements</var> is first executed with <var>simple-var</var>
bound to the first element of the list <var>list-expr</var>, then with <var>simple-var</var>
bound to the second element of <var>list-expr</var> and so on. <var>simple-var</var> must be a
simple variable,  it  must not   be  a list   element  selection
<code></code><var>list-var</var><code>[</code><var>int-expr</var><code>]</code>   or    a  record  component selection
<code></code><var>record-var</var><code>.</code><var>ident</var><code></code>.
<p>
The execution of the <code>for</code> loop over a list is exactly equivalent to
the following <code>while</code> loop.
<p>
<code></code><var>loop-list</var><code> := </code><var>list</var><code>;</code>
<br><code></code><var>loop-index</var><code> := 1;</code>
<br><code>while </code><var>loop-index</var><code> &lt;= Length(</code><var>loop-list</var><code>) do</code>
<br><code>  </code><var>variable</var><code> := </code><var>loop-list</var><code>[</code><var>loop-index</var><code>];</code>
<br><code>  </code><var>statements</var><code></code>
<br><code>  </code><var>loop-index</var><code> := </code><var>loop-index</var><code> + 1;</code>
<br><code>od;</code>
<p>
with the  exception  that <var>loop-list</var> and <var>loop-index</var> are different
variables for each <code>for</code> loop,
i.e., these variables of different <code>for</code> loops do not interfere with
each other.
<p>
The list <var>list-expr</var> is very often a range (see&nbsp;<a href="CHAP021.htm#SECT022">Ranges</a>).
<p>
<a name = "SSEC019.2"></a>
<li><code>for </code><var>variable</var><code> in [</code><var>from</var><code>..</code><var>to</var><code>] do </code><var>statements</var><code> od;</code>
<p>
corresponds to the more common
<p>
<code>for </code><var>variable</var><code> from </code><var>from</var><code> to </code><var>to</var><code> do </code><var>statements</var><code> od;</code>
<p>
in other programming languages.
<p>
<pre>
gap&gt; s := 0;;
gap&gt; for i in [1..100] do
&gt;    s := s + i;
&gt; od;
gap&gt; s;
5050
</pre>
<p>
Note in the following example how the modification of the <strong>list</strong> in the
loop body causes the loop body also to be executed for the new values.
<p>
<pre>
gap&gt; l := [ 1, 2, 3, 4, 5, 6 ];;
gap&gt; for i in l do
&gt;    Print( i, " " );
&gt;    if i mod 2 = 0 then Add( l, 3 * i / 2 ); fi;
&gt; od; Print( "\n" );
1 2 3 4 5 6 3 6 9 9 
gap&gt; l;
[ 1, 2, 3, 4, 5, 6, 3, 6, 9, 9 ]
</pre>
<p>
Note in the following example that the modification of the <strong>variable</strong>
that holds the list has no influence on the loop.
<p>
<pre>
gap&gt; l := [ 1, 2, 3, 4, 5, 6 ];;
gap&gt; for i in l do
&gt;    Print( i, " " );
&gt;    l := [];
&gt; od; Print( "\n" );
1 2 3 4 5 6 
gap&gt; l;
[  ]
</pre>
<p>
<a name = "SSEC019.3"></a>
<li><code>for </code><var>variable</var><code> in </code><var>iterator</var><code> do </code><var>statements</var><code> od;</code>
<p>
It is also possible to have a <code>for</code>-loop run over an iterator
(see&nbsp;<a href="CHAP028.htm#SECT007">Iterators</a>). In this case
the <code>for</code>-loop is equivalent to
<p>
<code>while not IsDoneIterator(</code><var>iterator</var><code>) do</code>
<br><code>  </code><var>variable</var><code> := NextIterator(</code><var>iterator</var><code>)</code>
<br><code>  </code><var>statements</var><code></code>
<br><code>od;</code>
<p>
<a name = "SSEC019.4"></a>
<li><code>for </code><var>variable</var><code> in </code><var>object</var><code> do </code><var>statements</var><code> od;</code>
<p>
Finally, if an object <var>object</var> which is not a list or an iterator appears in a
<code>for</code>-loop, then <font face="Gill Sans,Helvetica,Arial">GAP</font> will attempt to evaluate the function call
<code>Iterator(</code><var>object</var><code>)</code>. If this is successful then the loop is taken to
run over the iterator returned.
<p>
<pre>
gap&gt; g := Group((1,2,3,4,5),(1,2)(3,4)(5,6));
Group([ (1,2,3,4,5), (1,2)(3,4)(5,6) ])
gap&gt; count := 0;; sumord := 0;;
gap&gt; for x in g do
&gt; count := count + 1; sumord := sumord + Order(x); od;
gap&gt; count;
120
gap&gt; sumord;
471
</pre>
<p>
The effect of
<p>
<code>for </code><var>variable</var><code> in </code><var>domain</var><code> do</code>
<p>
should thus normally be the same as 
<p>
<code>for </code><var>variable</var><code> in AsList(</code><var>domain</var><code>) do</code>
<p>
but may use much less storage, as the iterator may be more compact than
a list of all the elements.
<p>
See <a href="CHAP028.htm#SECT007">Iterators</a> for details about iterators.
<p>
A <code>for</code> loop may be left prematurely using <code>break</code>, see <a href="CHAP004.htm#SECT020">Break</a>. This
combines especially well with a loop over an iterator, as a way of
searching through a domain for an element with some useful property.
<p>
<p>
<h2><a name="SECT020">4.20 Break</a></h2>
<p><p>
<a name = "I41"></a>

<a name = "SSEC020.1"></a>
<li><code>break;</code>
<p>
The statement <code>break;</code> causes an immediate exit from the innermost
loop enclosing it. It is an error to use this statement other than
inside a loop.
<p>
<pre>
gap&gt; g := Group((1,2,3,4,5),(1,2)(3,4)(5,6));
Group([ (1,2,3,4,5), (1,2)(3,4)(5,6) ])
gap&gt; for x in g do
&gt; if Order(x) = 3 then
&gt; break;
&gt; fi; od;
gap&gt; x;
(1,4,3)(2,6,5)
gap&gt; break;
A break statement can only appear inside a loop
</pre>
<p>
<p>
<h2><a name="SECT021">4.21 Continue</a></h2>
<p><p>
<a name = "I42"></a>

<a name = "SSEC021.1"></a>
<li><code>continue;</code>
<p>
The statement <code>continue;</code> causes the rest of the current iteration of
the innermost loop enclosing it to be skipped. It is an error to use
this statement other than inside a loop.
<p>
<pre>
gap&gt; g := Group((1,2,3),(1,2));
Group([ (1,2,3), (1,2) ])
gap&gt; for x in g do
&gt; if Order(x) = 3 then
&gt; continue;
&gt; fi; Print(x,"\n"); od;
()
(2,3)
(1,3)
(1,2)
gap&gt; continue;
A continue statement can only appear inside a loop
</pre>
<p>
<p>
<h2><a name="SECT022">4.22 Function</a></h2>
<p><p>
<a name = "I43"></a>

<a name = "I43"></a>
<a name = "I44"></a>

<a name = "I43"></a>
<a name = "I44"></a>
<a name = "I45"></a>

<a name = "I46"></a>

<a name = "I46"></a>
<a name = "I47"></a>

<a name = "I48"></a>

<a name = "I48"></a>
<a name = "I49"></a>

<a name = "SSEC022.1"></a>
<li><code>function( [ </code><var>arg-ident</var><code> {, </code><var>arg-ident</var><code>} ] )</code>
<br><code>    [local  </code><var>loc-ident</var><code> {, </code><var>loc-ident</var><code>} ; ]</code>
<br><code>    </code><var>statements</var><code></code>
<br><code>end</code>
<p>
A function is in fact a literal and not a statement. Such a function
literal can be assigned to a variable or to a list element or a record
component. Later this function can be called as described in <a href="CHAP004.htm#SECT010">Function Calls</a>.
<p>
The following is an example of a function definition.  It is a function
to compute values of the Fibonacci sequence (see <a href="CHAP017.htm#SSEC003.1">Fibonacci</a>).
<p>
<pre>
gap&gt; fib := function ( n )
&gt;     local f1, f2, f3, i;
&gt;     f1 := 1; f2 := 1;
&gt;     for i in [3..n] do
&gt;       f3 := f1 + f2;
&gt;       f1 := f2;
&gt;       f2 := f3;
&gt;     od;
&gt;     return f2;
&gt;   end;;
gap&gt; List( [1..10], fib );
[ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ]
</pre>
<p>
Because for each of the formal arguments <var>arg-ident</var> and for each of the
formal locals <var>loc-ident</var> a new variable is allocated when the function
is called (see <a href="CHAP004.htm#SECT010">Function Calls</a>), it is possible that a function calls
itself. This is usually called <strong>recursion</strong>. The following is a recursive
function that computes values of the Fibonacci sequence
<p>
<pre>
gap&gt; fib := function ( n )
&gt;     if n &lt; 3 then
&gt;       return 1;
&gt;     else
&gt;       return fib(n-1) + fib(n-2);
&gt;     fi;
&gt;   end;;
gap&gt; List( [1..10], fib );
[ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ]
</pre>
<p>
Note that the recursive version needs <code>2 * fib(</code><var>n</var><code>)-1</code> steps to compute
<code>fib(</code><var>n</var><code>)</code>, while the iterative version  of <code>fib</code> needs only <code></code><var>n</var><code>-2</code>
steps.  Both are not optimal however, the library function <code>Fibonacci</code>
only needs about <code>Log(</code><var>n</var><code>)</code> steps.
<p>
<a name = "I50"></a>

<a name = "I51"></a>

As noted in Section&nbsp;<a href="CHAP004.htm#SECT010">Function  Calls</a>,  the  case  where  a  function  is
defined with exactly one formal argument with the name <code>arg</code>, is special.
It provides a way of defining  a  function  with  a  variable  number  of
arguments; the values of all the actual arguments are stored  in  a  list
and this list is assigned to the new variable corresponding to the formal
argument <code>arg</code>. There are  two  typical  scenarios  for  wanting  such  a
possibility:  having  optional  arguments  and  having  any   number   of
arguments.
<p>
The  following  example  shows  one  way  that  the  function  <code>Position</code>
(see&nbsp;<a href="../ref/CHAP021.htm#SSEC016.1">Position</a>) might be encoded  and  demonstrates  the  ``optional
argument'' scenario.
<p>
<pre>
gap&gt; position := function ( arg )    
&gt;     local list, obj, pos;
&gt;     list := arg[1];
&gt;     obj  := arg[2];
&gt;     if 2 = Length(arg) then
&gt;       pos := 0;
&gt;     else
&gt;       pos := arg[3];
&gt;     fi;
&gt;     repeat
&gt;       pos := pos + 1;
&gt;       if pos &gt; Length(list) then
&gt;         return fail;
&gt;       fi;
&gt;     until list[pos] = obj;
&gt;     return pos;
&gt;    end;
function( arg ) ... end
gap&gt; position([1, 4, 2], 4);
2
gap&gt; position([1, 4, 2], 3);
fail
gap&gt; position([1, 4, 2], 4, 2);
fail
</pre>
<p>
The following  example  demonstrates  the  ``any  number  of  arguments''
scenario.
<p>
<pre>
gap&gt; sum := function ( arg )
&gt;     local total, x;
&gt;     total := 0;
&gt;     for x in arg do
&gt;       total := total + x;
&gt;     od;
&gt;     return total;
&gt;    end;
function( arg ) ... end
gap&gt; sum(1, 2, 3);
6
gap&gt; sum(1, 2, 3, 4);
10
gap&gt; sum();
0
</pre>
<p>
The user  should  compare  the  above  with  the  <font face="Gill Sans,Helvetica,Arial">GAP</font>  function  <code>Sum</code>
(see&nbsp;<a href="CHAP021.htm#SSEC020.24">Sum</a>) which, for example, may take a list argument  and  optionally
an initial element (which zero should the sum of an empty list return?).
<p>
Note that if a function <var>f</var> is defined as above with  the  single  formal
argument   <code>arg</code>   then   <code>NumberArgumentsFunction(</code><var>f</var><code>)</code>   returns   &#8722;1
(see&nbsp;<a href="CHAP005.htm#SSEC001.2">NumberArgumentsFunction</a>).
<p>
The argument <code>arg</code> when used as the single argument name of some function
<var>f</var> tells <font face="Gill Sans,Helvetica,Arial">GAP</font> that when it encounters <var>f</var> that it should form  a  list
out of the arguments of <var>f</var>. What if one wishes to do  the  ``opposite'':
tell <font face="Gill Sans,Helvetica,Arial">GAP</font> that a list should be ``unwrapped''  and  passed  as  several
arguments to a function. The function <code>CallFuncList</code> (see&nbsp;<a href="CHAP005.htm#SSEC002.1">CallFuncList</a>)
is provided for this purpose.
<p>
Also see Chapter&nbsp;<a href="CHAP005.htm">Functions</a>.
<p>
<a name = "I52"></a>

<a name = "SSEC022.2"></a>
<li><code></code><var>arg-ident</var><code> -&gt; </code><var>expr</var><code></code>
<p>
This is a shorthand for
<p>
<code>function ( </code><var>arg-ident</var><code> ) return </code><var>expr</var><code>; end.</code>
<p>
<var>arg-ident</var> must be a single identifier, i.e., it is not possible to
write functions of several arguments this way. Also <code>arg</code> is not treated
specially, so it is also impossible to write functions that take a
variable number of arguments this way.
<p>
The following is an example of a typical use of such a function
<p>
<pre>
gap&gt; Sum( List( [1..100], x -&gt; x^2 ) );
338350
</pre>
<p>
When the definition of a function <var>fun1</var> is evaluated inside another
function <var>fun2</var>,
<font face="Gill Sans,Helvetica,Arial">GAP</font> binds all the identifiers inside the function <var>fun1</var> that
are identifiers of an argument or a local of <var>fun2</var> to the corresponding
variable. This set of bindings is called the environment of the function
<var>fun1</var>. When <var>fun1</var> is called, its body is executed in this environment.
The following implementation of a simple stack uses this. Values can be
pushed onto the stack and then later be popped off again.  The
interesting thing here is that the functions <code>push</code> and <code>pop</code> in the
record returned by <code>Stack</code> access the local variable <code>stack</code> of <code>Stack</code>.
When <code>Stack</code> is called, a new variable for the identifier <code>stack</code> is
created.  When the function definitions of <code>push</code> and <code>pop</code> are then
evaluated (as part of the <code>return</code> statement) each reference to <code>stack</code>
is bound to this new variable. Note also that the two stacks <code>A</code> and <code>B</code>
do not interfere, because each call of <code>Stack</code> creates a new variable for
<code>stack</code>.
<p>
<pre>
gap&gt; Stack := function ()
&gt;     local  stack;
&gt;     stack := [];
&gt;     return rec(
&gt;       push := function ( value )
&gt;         Add( stack, value );
&gt;       end,
&gt;       pop := function ()
&gt;         local  value;
&gt;         value := stack[Length(stack)];
&gt;         Unbind( stack[Length(stack)] );
&gt;         return value;
&gt;       end
&gt;     );
&gt;  end;;
gap&gt; A := Stack();;
gap&gt; B := Stack();;
gap&gt; A.push( 1 ); A.push( 2 ); A.push( 3 );
gap&gt; B.push( 4 ); B.push( 5 ); B.push( 6 );
gap&gt; A.pop(); A.pop(); A.pop();
3
2
1
gap&gt; B.pop(); B.pop(); B.pop();
6
5
4
</pre>
<p>
This feature should be used rarely, since its implementation in <font face="Gill Sans,Helvetica,Arial">GAP</font> is
not very efficient.
<p>
<p>
<h2><a name="SECT023">4.23 Return</a></h2>
<p><p>
<a name = "SSEC023.1"></a>
<li><code>return;</code>
<p>
In this form <code>return</code> terminates the call of the innermost function that
is currently executing, and control returns to the calling function. An
error is signalled if no function is currently executing. No value is
returned by the function.
<p>
<a name = "SSEC023.2"></a>
<li><code>return </code><var>expr</var><code>;</code>
<p>
In this form <code>return</code> terminates the call of the innermost function that
is currently executing, and returns the value of the expression <var>expr</var>.
Control returns to the calling function. An error is signalled if no
function is currently executing.
<p>
Both statements can also be used in break loops (see <a href="CHAP006.htm#SECT004">Break Loops</a>).
<code>return;</code> has the effect that the computation continues where it was
interrupted by an error or the user hitting <var>ctr</var>-<code>C</code>.  <code>return </code><var>expr</var><code>;</code>
can be used to continue execution after an error. What happens with the
value <var>expr</var> depends on the particular error.
<p>
For examples of <code>return</code> statements, see the functions <code>fib</code> and <code>Stack</code>
in Chapter&nbsp;<a href="CHAP005.htm">Functions</a>.
<p>
<p>
<h2><a name="SECT024">4.24 The Syntax in BNF</a></h2>
<p><p>
<a name = "I53"></a>

This section contains the definition of the <font face="Gill Sans,Helvetica,Arial">GAP</font> syntax in
Backus-Naur form. A few recent additions to the syntax may be missing
from this definition. Also, the actual rules for identifier names
implemented by the system, are somewhat more permissive than those
given below (see section <a href="CHAP004.htm#SECT006">Identifiers</a>).
<p>
A  BNF is a set of rules, whose left side  is the name of  a  syntactical
construct.  Those  names  are enclosed in angle  brackets and  written in
<var>italics</var>.  The right side of each rule contains a possible form for that
syntactic  construct.   Each  right  side  may  contain  names  of  other
syntactic  constructs,  again  enclosed in angle brackets and written  in
<var>italics</var>,  or character  sequences that  must  occur literally; they are
written in <code>typewriter style</code>.
<p>
Furthermore  each righthand side  can contain  the  following metasymbols
written in <strong>boldface</strong>.  If the right  hand  side contains forms separated
by a pipe symbol  (&#124;)  this means  that one  of the possible forms can
occur.  If a part of a form  is enclosed  in square brackets  ([ ])  this
means that this part is optional, i.e.  might be present or  missing.  If
part  of the form is enclosed  in curly braces  (<code>{  }</code>)  this means that
the part may occur arbitrarily often, or possibly be missing.
<p>

<table><tr><td width=80>
&lt;Permutation&#62; </td><td width=11>
:= </td><td width=350>
`(' &lt;Expr&#62; {`,' &lt;Expr&#62; } `)'
                { `(' &lt;Expr&#62; {`,' &lt;Expr&#62; } `)' }</td></table>

<table><tr><td width=80>
&lt;Ident&#62;       </td><td width=11>
:=  </td><td width=350>
`a'<font face=symbol>|</font
>...<font face=symbol>|</font
>`z'<font face=symbol>|</font
>`A'<font face=symbol>|</font
>...<font face=symbol>|</font
>`Z'<font face=symbol>|</font
>`_'
{`a'<font face=symbol>|</font
>...<font face=symbol>|</font
>`z'<font face=symbol>|</font
>`A'<font face=symbol>|</font
>...<font face=symbol>|</font
>`Z'<font face=symbol>|</font
>`0'<font face=symbol>|</font
>...<font face=symbol>|</font
>`9'<font face=symbol>|</font
>`_'}</td></table>

<table><tr><td width=80>
&lt;Var&#62;         </td><td width=11>
:=  </td><td width=350>
&lt;Ident&#62;</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
&lt;Var&#62; `.' &lt;Ident&#62;</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
&lt;Var&#62; `.' `(' &lt;Expr&#62; `)'</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
&lt;Var&#62; `[' &lt;Expr&#62; `]'</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
&lt;Var&#62; `{ &lt;Expr&#62; }'</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
&lt;Var&#62; `(' [ &lt;Expr&#62; { ,&lt;Expr&#62; } ] `)'</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
&lt;Var&#62; `!.' &lt;Ident&#62;</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
&lt;Var&#62; `!.' `(' &lt;Expr&#62; `)'</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
&lt;Var&#62; `![' &lt;Expr&#62; `]'</td></table>

<table><tr><td width=80>
&lt;List&#62;        </td><td width=11>
:=  </td><td width=350>
`[' [ &lt;Expr&#62; ] {`,' [ &lt;Expr&#62; ] } `]'</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
`[' &lt;Expr&#62; [, &lt;Expr&#62; ] `..' &lt;Expr&#62; `]'</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
&lt;List&#62; `' &lt;List&#62; `'</td></table>

<table><tr><td width=80>
&lt;Record&#62;      </td><td width=11>
:=  </td><td width=350>
`rec(' [ &lt;Ident&#62; `:=' &lt;Expr&#62;
                         {`,' &lt;Ident&#62; `:=' &lt;Expr&#62; } ] `)'</td></table>

<table><tr><td width=80>
&lt;Permutation&#62; </td><td width=11>
:=  </td><td width=350>
`(' &lt;Expr&#62; {`,' &lt;Expr&#62; } `)'
                    { `(' &lt;Expr&#62; {`,' &lt;Expr&#62; } `)' }</td></table>

<table><tr><td width=80>
&lt;Function&#62;    </td><td width=11>
:=  </td><td width=350>
`function (' [ &lt;Ident&#62; {`,' &lt;Ident&#62; } ] `)'</td></table>

<table><tr><td width=80>
              </td><td width=11>
    </td><td width=350>
    [ `local'  &lt;Ident&#62; {`,' &lt;Ident&#62; } `;' ]</td></table>

<table><tr><td width=80>
              </td><td width=11>
    </td><td width=350>
    &lt;Statements&#62;</td></table>

<table><tr><td width=80>
              </td><td width=11>
    </td><td width=350>
`end'</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
 &lt;Ident&#62; `-&#62;' &lt;Expr&#62;</td></table>

<table><tr><td width=80>
&lt;Char&#62;        </td><td width=11>
:=  </td><td width=350>
'&lt;any character&#62; '</td></table>

<table><tr><td width=80>
&lt;String&#62;      </td><td width=11>
:=  </td><td width=350>
`"' { &lt;any character&#62; } `"'</td></table>

<table><tr><td width=80>
&lt;Int&#62;         </td><td width=11>
:=  </td><td width=350>
`0'<font face=symbol>|</font
>`1'<font face=symbol>|</font
>...<font face=symbol>|</font
>`9'
                    {`0'<font face=symbol>|</font
>`1'<font face=symbol>|</font
>...<font face=symbol>|</font
>`9'}</td></table>

<table><tr><td width=80>
&lt;Atom&#62;        </td><td width=11>
:=  </td><td width=350>
&lt;Int&#62;</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
&lt;Var&#62;</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
`(' &lt;Expr&#62; `)'</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
&lt;Permutation&#62;</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
&lt;Char&#62;</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
&lt;String&#62;</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
&lt;Function&#62;</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
&lt;List&#62;</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
&lt;Record&#62;</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
{ `not' } `true'</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
{ `not' } `false'</td></table>

<table><tr><td width=80>
&lt;Factor&#62;      </td><td width=11>
:=  </td><td width=350>
{`+'<font face=symbol>|</font
>`-'} &lt;Atom&#62;
                [ `^' {`+'<font face=symbol>|</font
>`-'} &lt;Atom&#62; ]</td></table>

<table><tr><td width=80>
&lt;Term&#62;        </td><td width=11>
:=  </td><td width=350>
&lt;Factor&#62; { `*'<font face=symbol>|</font
>`/'<font face=symbol>|</font
>`mod' &lt;Factor&#62; }</td></table>

<table><tr><td width=80>
&lt;Arith&#62;       </td><td width=11>
:=  </td><td width=350>
&lt;Term&#62; { `+'<font face=symbol>|</font
>`-' &lt;Term&#62; }</td></table>

<table><tr><td width=80>
&lt;Rel&#62;         </td><td width=11>
:=  </td><td width=350>
{ `not' } &lt;Arith&#62;
                [ `='<font face=symbol>|</font
>`\&lt;&#62;'<font face=symbol>|</font
>`\&lt;'<font face=symbol>|</font
>`&#62;'<font face=symbol>|</font
>`\&lt;='<font face=symbol>|</font
>`&#62;='<font face=symbol>|</font
>`in' &lt;Arith&#62; ]</td></table>

<table><tr><td width=80>
&lt;And&#62;         </td><td width=11>
:=  </td><td width=350>
&lt;Rel&#62; { `and' &lt;Rel&#62; }</td></table>

<table><tr><td width=80>
&lt;Logical&#62;     </td><td width=11>
:=  </td><td width=350>
&lt;And&#62; { `or' &lt;And&#62; }</td></table>

<table><tr><td width=80>
&lt;Expr&#62;        </td><td width=11>
:=  </td><td width=350>
&lt;Logical&#62;</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
&lt;Var&#62;</td></table>

<table><tr><td width=80>
&lt;Statement&#62;   </td><td width=11>
:=  </td><td width=350>
&lt;Expr&#62;</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
&lt;Var&#62; `:=' &lt;Expr&#62;</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
`if'   &lt;Expr&#62;  `then' &lt;Statements&#62;</td></table>

<table><tr><td width=80>
              </td><td width=11>
    </td><td width=150>
{ `elif' &lt;Expr&#62;  `then' </td><td width=350>
&lt;Statements&#62; }</td></table>

<table><tr><td width=80>
              </td><td width=11>
    </td><td width=150>
[ `else'</td><td width=350>
&lt;Statements&#62;  ] `fi'</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
`for' &lt;Var&#62; `in' &lt;Expr&#62; `do' &lt;Statements&#62; `od'</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
`while' &lt;Expr&#62;  `do' &lt;Statements&#62;  `od'</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
`repeat' &lt;Statements&#62;  `until' &lt;Expr&#62;</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
`return' [ &lt;Expr&#62; ]</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
`break'</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
`quit'</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
`QUIT'</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
</td></table>

<table><tr><td width=80>
&lt;Statements&#62;  </td><td width=11>
:=  </td><td width=350>
{ &lt;Statement&#62; `;' }</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
`;'</td></table>

<table><tr><td width=80>
              </td><td width=11>
<font face=symbol>|</font
> </td><td width=350>
</td></table>
<p>
<p>
[<a href="../index.htm">Top</a>] [<a href = "chapters.htm">Up</a>] [<a href ="CHAP003.htm">Previous</a>] [<a href ="CHAP005.htm">Next</a>] [<a href = "theindex.htm">Index</a>]
<P>
<font face="Gill Sans,Helvetica,Arial">GAP 4 manual<br>December 2008
</font></body></html>