
CREATE FUNCTION hello (s CHAR(20))
  RETURNS CHAR(50) DETERMINISTIC
  RETURN CONCAT('Hello, ',s,'!');

SELECT hello('world');
+----------------+
| hello('world') |
+----------------+
| Hello, world!  |
+----------------+

You can use a compound statement in a function to manipulate data with
statements like INSERT and UPDATE. The following example creates a counter
function that uses a temporary table to store the current value. Because the
compound statement contains statements terminated with semicolons, you have to
first change the statement delimiter with the DELIMITER statement to allow the
semicolon to be used in the function body. See Delimiters in the mysql client
for more.

CREATE TEMPORARY TABLE counter (c INT);
INSERT INTO counter VALUES (0);
DELIMITER //
CREATE FUNCTION counter () RETURNS INT
 BEGIN
  UPDATE counter SET c = c + 1;
  RETURN (SELECT c FROM counter LIMIT 1);
 END //
DELIMITER ;

Character set and collation:

CREATE FUNCTION hello2 (s CHAR(20))
 RETURNS CHAR(50) CHARACTER SET 'utf8' COLLATE 'utf8_bin' DETERMINISTIC
 RETURN CONCAT('Hello, ',s,'!');

URL: https://mariadb.com/kb/en/create-function/