USE test
DELIMITER $$
DROP FUNCTION IF EXISTS gcd$$
CREATE FUNCTION gcd(a INT, b INT)
RETURNS INT

BEGIN
  DECLARE x0 INT DEFAULT abs(a);
  DECLARE x1 INT DEFAULT abs(b);
  DECLARE r INT;
  WHILE x1 != 0 DO
    SET r = x0 MOD x1;
    SET x0 = x1;
    SET x1 = r;
  END WHILE;
  RETURN x0;
END $$
DELIMITER ;
