[GAP Forum] Default Arguments

Kieran Roberts kroberts at math.uni-bielefeld.de
Fri Feb 5 08:54:41 GMT 2016


Dear GAP forum:

Is it possible to include default arguments in a function?

Such as something like this (which doesn't work):

add := function(a,b:=1)
    return a+b;
end;

So that add(2,2) = 4 and add(2) = 3. The only way I've managed to do
is by writing:

add := function(arg)
    if Size(arg) = 2 then
        return arg[1] + arg[2];
    else
        return arg[1] + 1;
    fi;
end;

But if you have 2 or 3 (optional) arguments with default values this
becomes cumbersome. Is there a more efficient way?

Best,
Kieran.



More information about the Forum mailing list