Sub topic 4
Parameters: Named, Default, OUT, and INOUT
Assume you know basic SQL, PostgreSQL data types, and PL/pgSQL block structure. This chapter focuses on how values enter and leave a function. Choosing parameter modes carefully makes a function easier to call and harder to misuse.
Input parameters
An IN parameter is an input value. IN is the default mode, so these declarations are equivalent:
CREATE FUNCTION discount_a(amount numeric)
RETURNS numeric
LANGUAGE plpgsql
AS $$
BEGIN
RETURN amount * 0.9;
END;
$$;
CREATE FUNCTION discount_b(IN amount numeric)
RETURNS numeric
LANGUAGE plpgsql
AS $$
BEGIN
RETURN amount * 0.9;
END;
$$;Parameter names become variables inside the function. Use names that do not conflict with column names. A prefix such as p_ is a common convention for parameters.
Default values and named notation
A parameter can have a default value. Callers may omit that argument, so defaults are useful for optional settings.
CREATE FUNCTION add_tax(
p_price numeric,
p_rate numeric DEFAULT 0.20
)
RETURNS numeric
LANGUAGE plpgsql
IMMUTABLE
AS $$
BEGIN
RETURN round(p_price * (1 + p_rate), 2);
END;
$$;
SELECT add_tax(100);
SELECT add_tax(100, 0.07);
SELECT add_tax(p_price => 100, p_rate => 0.07);The first call uses the default rate of 0.20. Named notation uses parameter_name => value, which makes calls clearer when a function has several similar arguments.
You can mix positional and named notation, but positional arguments must come first:
SELECT add_tax(100, p_rate => 0.07);After an input parameter has a default, later input parameters must also have defaults. Otherwise, PostgreSQL could not determine which omitted argument a supplied value represents.
| Parameter form | Purpose | Caller must provide it? |
|---|---|---|
| `IN p_id integer` | Receives an input value | Yes, unless it has a default |
| `OUT p_total numeric` | Produces an output value | No |
| `INOUT p_value integer` | Receives and then returns a value | Yes, unless it has a default |
OUT parameters
An OUT parameter is a result variable. The caller does not supply it. PostgreSQL returns it when the function finishes.
CREATE FUNCTION customer_label(
p_first_name text,
p_last_name text,
OUT label text
)
LANGUAGE plpgsql
IMMUTABLE
AS $$
BEGIN
label := trim(p_first_name || ' ' || p_last_name);
END;
$$;
SELECT * FROM customer_label('Ada', 'Lovelace');The OUT parameter also defines the function's result column name and type. Inside the function, assign to label instead of using RETURN label. A function with one OUT parameter returns one value. Multiple OUT parameters return one composite row.
CREATE FUNCTION split_total(
p_total numeric,
OUT whole numeric,
OUT remainder numeric
)
LANGUAGE plpgsql
IMMUTABLE
AS $$
BEGIN
whole := trunc(p_total);
remainder := p_total - whole;
END;
$$;
SELECT * FROM split_total(12.75);INOUT parameters
An INOUT parameter combines both roles. The caller supplies its initial value, and the function returns the changed value.
CREATE FUNCTION add_fee(
INOUT p_amount numeric,
p_fee numeric DEFAULT 5
)
LANGUAGE plpgsql
IMMUTABLE
AS $$
BEGIN
p_amount := p_amount + p_fee;
END;
$$;
SELECT * FROM add_fee(100);
SELECT * FROM add_fee(100, 12.50);Use INOUT when the operation naturally transforms one value. For ordinary calculations, a regular input parameter with a scalar RETURNS clause is often clearer.
Exercise
What does SELECT add_tax(p_price => 100); use for the tax rate, and why?
Show answer
It uses the default value 0.20, because the call supplies p_price but omits p_rate.