[Top] [Up] [Previous] [Next] [Index]

# 5 Functions

### Sections

1. Information about a function
2. Calling a function with a list argument that is interpreted as several arguments
3. Functions that do nothing
4. Function Types

The section Function 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.

## 5.1 Information about a function

- `NameFunction(` func`) F`

  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 different from the name that is documented.) If no such name exists, `"unknown"` is returned.

      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"

- `NumberArgumentsFunction(` func`) F`

  returns the number of arguments the function func accepts. For functions that use `arg` to take a variable number of arguments, as well as for operations, -1 is returned. For attributes, 1 is returned.

      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

- `NamesLocalVariablesFunction(` func`) F`

  returns a mutable list of strings; the first entries are the names of the arguments of the function func, in the same order as they were entered in the definition of func, and the remaining ones are the local variables as given in the `local` statement in func. (The number of arguments can be computed with `NumberArgumentsFunction`.)

      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

  ## 5.2 Calling a function with a list argument that is interpreted as several arguments

- `CallFuncList(` func`,` args`) F`

  returns the result, when calling function func with the arguments given in the list args, i.e. args is ``unwrapped'' so that args appears as several arguments to func.

      gap> CallFuncList(\+, [6, 7]);
      13
      gap> #is equivalent to:
      gap> \+(6, 7);
      13

  A more useful application of `CallFuncList` is for a function g that is called in the body of a function f with (a sublist of) the arguments of f, where f has been defined with a single formal argument `arg` (see function); see the following code fragment.

      f := function ( arg )
             CallFuncList(g, arg);
             ...
           end;

  In the body of f the several arguments passed to f become a list `arg`. If g were called instead via ``g`( arg )` then g would see a single list argument, so that g would, in general, have to ``unwrap'' the passed list. The following (not particularly useful) example demonstrates both described possibilities for the call to g.

      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 ]

  ## 5.3 Functions that do nothing

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

- `ReturnTrue( ... ) F`

  This function takes any number of arguments, and always returns `true`.

- `ReturnFalse( ... ) F`

  This function takes any number of arguments, and always returns `false`.

- `ReturnFail( ... ) F`

  This function takes any number of arguments, and always returns `fail`.

- `IdFunc(` obj`) F`

  returns obj.

  ## 5.4 Function Types

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

- `IsFunction(` obj`) C`

  is the category of functions.

- `IsOperation(` obj`) C`

  is the category of operations. Every operation is a function, but not vice versa.

- `FunctionsFamily V`

  is the family of all functions.

  [Top] [Up] [Previous] [Next] [Index]

  GAP 4 manual
  October 2007