Error
Error Code: 1320

MySQL Error 1320: Missing FUNCTION RETURN

📦 MySQL
📋

Description

This error occurs when you attempt to create or execute a MySQL stored function that does not contain a mandatory `RETURN` statement. Unlike stored procedures, functions are designed to compute and return a single value of a specified data type, and the absence of a `RETURN` statement makes the function definition invalid.
💬

Error Message

No RETURN found in FUNCTION %s
🔍

Known Causes

3 known causes
⚠️
Missing RETURN Statement
The primary cause is the complete absence of a `RETURN` statement within the body of the stored function, which is required for all MySQL functions.
⚠️
Incomplete Conditional Logic
A `RETURN` statement might be present, but it's nested within conditional logic (e.g., `IF...THEN`) such that not all possible execution paths within the function lead to a `RETURN` statement.
⚠️
Confusing Function with Procedure
Developers sometimes confuse the syntax and requirements for a `FUNCTION` (which *must* return a value) with a `PROCEDURE` (which does not return a value and does not use `RETURN`).
🛠️

Solutions

3 solutions available

1. Add a RETURN Statement to Your Function easy

Ensure every execution path within the function explicitly returns a value.

1
Review the definition of the stored function that is causing the error (the function name will be in the error message). Identify all possible execution paths, including conditional statements (IF, CASE) and loops.
2
For each path, add a `RETURN` statement with an appropriate value. If a path might not naturally produce a value, consider returning a default value (e.g., NULL) or raising an error if that path should be impossible.
sql
DELIMITER //

CREATE FUNCTION my_function(param INT) RETURNS INT
BEGIN
  DECLARE result INT;
  IF param > 10 THEN
    SET result = param * 2;
  ELSE
    SET result = param + 5;
  END IF;

  RETURN result; -- Ensure a RETURN statement is present
END //

DELIMITER ;
3
If you are using `CASE` statements, make sure each `WHEN` clause, and potentially an `ELSE` clause, leads to a `RETURN` statement.
sql
DELIMITER //

CREATE FUNCTION categorize_value(value INT) RETURNS VARCHAR(50)
BEGIN
  DECLARE category VARCHAR(50);
  CASE
    WHEN value < 0 THEN
      SET category = 'Negative';
    WHEN value = 0 THEN
      SET category = 'Zero';
    ELSE
      SET category = 'Positive';
  END CASE;

  RETURN category; -- RETURN is outside the CASE but handles all possibilities
END //

DELIMITER ;

2. Handle All Conditional Branches medium

Explicitly cover all outcomes of IF or CASE statements.

1
Examine the logic within your function. If there are `IF` or `CASE` statements, ensure that every possible condition leads to a `RETURN` statement. Sometimes, a branch might be missed, especially if the `ELSE` part of an `IF` statement doesn't have a `RETURN`.
2
Add a `RETURN` statement at the end of the function that acts as a fallback, returning a default value (like `NULL` if the return type allows) if no other `RETURN` statement has been executed. This is crucial for ensuring a value is always returned.
sql
DELIMITER //

CREATE FUNCTION find_item_price(item_id INT) RETURNS DECIMAL(10,2)
BEGIN
  DECLARE price DECIMAL(10,2);

  SELECT price INTO price FROM products WHERE id = item_id;

  IF price IS NULL THEN
    -- Handle cases where the item_id might not exist
    -- Option 1: Return NULL
    RETURN NULL;
    -- Option 2: Return a default price (if appropriate)
    -- RETURN 0.00;
    -- Option 3: Signal an error (if the function should not proceed)
    -- SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Item not found';
  END IF;

  RETURN price;
END //

DELIMITER ;

3. Simplify Function Logic to Ensure Return medium

Refactor complex logic to make it easier to guarantee a return path.

1
If your function has very complex nested `IF` statements or intricate loops, it can be difficult to visually verify that every path includes a `RETURN`. Consider simplifying the logic.
2
Break down complex logic into smaller, more manageable parts. You might even consider creating helper functions if a piece of logic is repeated or particularly convoluted.
3
After simplification, re-apply the steps from the previous solutions to ensure a `RETURN` statement is present for all logical outcomes.
sql
-- Original complex logic (example)
-- CREATE FUNCTION complex_calc(a INT, b INT) RETURNS INT
-- BEGIN
--   IF a > 10 THEN
--     IF b < 5 THEN
--       -- Missing RETURN here
--     ELSE
--       RETURN a * b;
--     END IF;
--   ELSE
--     RETURN a + b;
--   END IF;
-- END //

-- Simplified and corrected logic
DELIMITER //

CREATE FUNCTION simple_calc(a INT, b INT) RETURNS INT
BEGIN
  DECLARE result INT;

  IF a > 10 THEN
    IF b < 5 THEN
      SET result = a - b; -- Added RETURN path
    ELSE
      SET result = a * b;
    END IF;
  ELSE
    SET result = a + b;
  END IF;

  RETURN result; -- Added a final RETURN to cover all paths
END //

DELIMITER ;
🔗

Related Errors

5 related errors