<html><head><title>[ref] 5 Functions</title></head> <body text="#000000" bgcolor="#ffffff"> [<a href="../index.htm">Top</a>] [<a href = "chapters.htm">Up</a>] [<a href ="CHAP004.htm">Previous</a>] [<a href ="CHAP006.htm">Next</a>] [<a href = "theindex.htm">Index</a>] <h1>5 Functions</h1><p> <P> <H3>Sections</H3> <oL> <li> <A HREF="CHAP005.htm#SECT001">Information about a function</a> <li> <A HREF="CHAP005.htm#SECT002">Calling a function with a list argument that is interpreted as several arguments</a> <li> <A HREF="CHAP005.htm#SECT003">Functions that do nothing</a> <li> <A HREF="CHAP005.htm#SECT004">Function Types</a> </ol><p> <p> <a name = "I0"></a> The section <a href="CHAP004.htm#SSEC022.1">Function</a> 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. <p> <p> <h2><a name="SECT001">5.1 Information about a function</a></h2> <p><p> <a name = "SSEC001.1"></a> <li><code>NameFunction( </code><var>func</var><code> ) F</code> <p> returns the name of a function. For operations, this is the name used in their declaration. For functions, this is the variable name they were first assigned to. (For some internal functions, this might be a name <strong>different</strong> from the name that is documented.) If no such name exists, <code>"unknown"</code> is returned. <p> <pre> gap> NameFunction(SylowSubgroup); "SylowSubgroup" gap> Blubberflutsch:=x->x;; gap> NameFunction(Blubberflutsch); "Blubberflutsch" gap> a:=Blubberflutsch;; gap> NameFunction(a); "Blubberflutsch" gap> NameFunction(x->x); "unknown" gap> NameFunction(NameFunction); "NAME_FUNC" </pre> <p> <a name = "SSEC001.2"></a> <li><code>NumberArgumentsFunction( </code><var>func</var><code> ) F</code> <p> returns the number of arguments the function <var>func</var> accepts. For functions that use <code>arg</code> to take a variable number of arguments, as well as for operations, -1 is returned. For attributes, 1 is returned. <p> <pre> gap> NumberArgumentsFunction(function(a,b,c,d,e,f,g,h,i,j,k)return 1;end); 11 gap> NumberArgumentsFunction(Size); 1 gap> NumberArgumentsFunction(IsCollsCollsElms); 3 gap> NumberArgumentsFunction(Sum); -1 </pre> <p> <a name = "SSEC001.3"></a> <li><code>NamesLocalVariablesFunction( </code><var>func</var><code> ) F</code> <p> returns a mutable list of strings; the first entries are the names of the arguments of the function <var>func</var>, in the same order as they were entered in the definition of <var>func</var>, and the remaining ones are the local variables as given in the <code>local</code> statement in <var>func</var>. (The number of arguments can be computed with <code>NumberArgumentsFunction</code>.) <p> <pre> gap> NamesLocalVariablesFunction( function( a, b ) local c; return 1; end ); [ "a", "b", "c" ] gap> NamesLocalVariablesFunction( function( arg ) local a; return 1; end ); [ "arg", "a" ] gap> NamesLocalVariablesFunction( Size ); fail </pre> <p> <p> <h2><a name="SECT002">5.2 Calling a function with a list argument that is interpreted as several arguments</a></h2> <p><p> <a name = "SSEC002.1"></a> <li><code>CallFuncList( </code><var>func</var><code>, </code><var>args</var><code> ) F</code> <p> returns the result, when calling function <var>func</var> with the arguments given in the list <var>args</var>, i.e. <var>args</var> is ``unwrapped'' so that <var>args</var> appears as several arguments to <var>func</var>. <p> <pre> gap> CallFuncList(\+, [6, 7]); 13 gap> #is equivalent to: gap> \+(6, 7); 13 </pre> <p> A more useful application of <code>CallFuncList</code> is for a function <var>g</var> that is called in the body of a function <var>f</var> with (a sublist of) the arguments of <var>f</var>, where <var>f</var> has been defined with a single formal argument <code>arg</code> (see <a href="CHAP004.htm#SSEC022.1">function</a>); see the following code fragment. <p> <pre> f := function ( arg ) CallFuncList(g, arg); ... end; </pre> <p> In the body of <var>f</var> the several arguments passed to <var>f</var> become a list <code>arg</code>. If <var>g</var> were called instead via <code></code><var>g</var><code>( arg )</code> then <var>g</var> would see a single list argument, so that <var>g</var> would, in general, have to ``unwrap'' the passed list. The following (not particularly useful) example demonstrates both described possibilities for the call to <var>g</var>. <p> <pre> gap> PrintNumberFromDigits := function ( arg ) > CallFuncList( Print, arg ); > Print( "\n" ); > end; function( arg ) ... end gap> PrintNumberFromDigits( 1, 9, 7, 3, 2 ); 19732 gap> PrintDigits := function ( arg ) > Print( arg ); > Print( "\n" ); > end; function( arg ) ... end gap> PrintDigits( 1, 9, 7, 3, 2 ); [ 1, 9, 7, 3, 2 ] </pre> <p> <p> <h2><a name="SECT003">5.3 Functions that do nothing</a></h2> <p><p> 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 <code>ReturnTrue</code> is often used as family predicate in <code>InstallMethod</code> (see <a href="../prg/CHAP002.htm#SSEC002.1">InstallMethod</a> in ``Programming in <font face="Gill Sans,Helvetica,Arial">GAP</font>''). <p> <a name = "SSEC003.1"></a> <li><code>ReturnTrue( ... ) F</code> <p> This function takes any number of arguments, and always returns <code>true</code>. <p> <a name = "SSEC003.2"></a> <li><code>ReturnFalse( ... ) F</code> <p> This function takes any number of arguments, and always returns <code>false</code>. <p> <a name = "SSEC003.3"></a> <li><code>ReturnFail( ... ) F</code> <p> This function takes any number of arguments, and always returns <code>fail</code>. <p> <a name = "SSEC003.4"></a> <li><code>IdFunc( </code><var>obj</var><code> ) F</code> <p> returns <var>obj</var>. <p> <p> <h2><a name="SECT004">5.4 Function Types</a></h2> <p><p> Functions are <font face="Gill Sans,Helvetica,Arial">GAP</font> objects and thus have categories and a family. <p> <a name = "SSEC004.1"></a> <li><code>IsFunction( </code><var>obj</var><code> ) C</code> <p> is the category of functions. <p> <a name = "SSEC004.2"></a> <li><code>IsOperation( </code><var>obj</var><code> ) C</code> <p> is the category of operations. Every operation is a function, but not vice versa. <p> <a name = "SSEC004.3"></a> <li><code>FunctionsFamily V</code> <p> is the family of all functions. <p> <p> [<a href="../index.htm">Top</a>] [<a href = "chapters.htm">Up</a>] [<a href ="CHAP004.htm">Previous</a>] [<a href ="CHAP006.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>