4.14 Assignments

  • var := expr;

    The assignment has the effect of assigning the value of the expressions expr to the variable var.

    The variable var may be an ordinary variable (see Variables), a list element selection list-var[int-expr] (see List Assignment) or a record component selection record-var.ident (see Record Assignment). 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.

    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.

    gap> data:= rec( numbers:= [ 1, 2, 3 ] );
    rec(
     numbers := [ 1, 2, 3 ] )
    gap> data.string:= "string";; data;
    rec(
      numbers := [ 1, 2, 3 ],
      string := "string" )
    gap> data.numbers[2]:= 4;; data;
    rec(
      numbers := [ 1, 4, 3 ],
      string := "string" )
    

    If the expression expr 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 Break Loops). As usual you can leave the break loop with quit;. If you enter return return-expr; the value of the expression return-expr is assigned to the variable, and execution continues after the assignment.

    gap> f1:= function( x ) Print( "value: ", x, "\n" ); end;;
    gap> f2:= function( x ) return f1( x ); end;;
    gap> f2( 4 );
    value: 4
    Function Calls: <func> must return a value at
    return f1( x );
    <function>( <arguments> ) called from read-eval-loop
    Entering break read-eval-print loop, you can 'quit;' to quit to outer loop,
    or you can return a value for the result to continue
    brk> return "hello";
    "hello"
    

    In the above example, the function f2 calls f1 with argument 4, and since f1 does not return a value (but only prints a line ``value: x''), the return statement of f2 cannot be executed. The error message says that it is possible to return an appropriate value, and the returned string "hello" is used by f2 instead of the missing return value of f1.

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

    GAP 4 manual
    February 2000