Is this what you're looking for?

gap> LessThanFiveHundred:=function(X,Y)

> if X+Y > 500 then

> Print("X+Y cannot be greater than 500 \n");

> else

> while (3*X + Y)>500 do

> X:=X-1;

> Y:=Y+1;

> od;

> fi;

> Print("X = ", X, "\n");

> Print("Y = ", Y, "\n");

> Print(" 3*X + Y = ");

> return 3*X + Y;

> end;