be used to exit the function
early; but do not write an expression following
<command>RETURN</command>.
</para>
<para>
The return value of a function cannot be left undefined. If
control reaches the end of the top-level block of the function
without hitting a <command>RETURN</command> statement, a run-time
error will occur. This restriction does not apply to functions
with output parameters and functions returning <type>void</type>,
however. In those cases a <command>RETURN</command> statement is
automatically executed if the top-level block finishes.
</para>
<para>
Some examples:
<programlisting>
-- functions returning a scalar type
RETURN 1 + 2;
RETURN scalar_var;
-- functions returning a composite type
RETURN composite_type_var;
RETURN (1, 2, 'three'::text); -- must cast columns to correct types
</programlisting>
</para>
</sect3>
<sect3 id="plpgsql-statements-returning-return-next">
<title><command>RETURN NEXT</command> and <command>RETURN QUERY</command></title>
<indexterm>
<primary>RETURN NEXT</primary>
<secondary>in PL/pgSQL</secondary>
</indexterm>
<indexterm>
<primary>RETURN QUERY</primary>
<secondary>in PL/pgSQL</secondary>
</indexterm>
<synopsis>
RETURN NEXT <replaceable>expression</replaceable>;
RETURN QUERY <replaceable>query</replaceable>;
RETURN QUERY EXECUTE <replaceable class="command">command-string</replaceable> <optional> USING <replaceable>expression</replaceable> <optional>, ... </optional> </optional>;
</synopsis>
<para>
When a <application>PL/pgSQL</application> function is declared to return
<literal>SETOF <replaceable>sometype</replaceable></literal>, the procedure
to follow is slightly different. In that case, the individual
items to return are specified by a sequence of <command>RETURN
NEXT</command> or <command>RETURN QUERY</command> commands, and
then a final <command>RETURN</command> command with no argument
is used to indicate that the function has finished executing.
<command>RETURN NEXT</command> can be used with both scalar and
composite data types; with a composite result type, an entire
<quote>table</quote> of results will be returned.
<command>RETURN QUERY</command> appends the results of executing
a query to the function's result set. <command>RETURN
NEXT</command> and <command>RETURN QUERY</command> can be freely
intermixed in a single set-returning function, in which case
their results will be concatenated.
</para>
<para>
<command>RETURN NEXT</command> and <command>RETURN
QUERY</command> do not actually return from the function —
they simply append zero or more rows to the function's result
set. Execution then continues with the next statement in the
<application>PL/pgSQL</application> function. As successive
<command>RETURN NEXT</command> or <command>RETURN
QUERY</command> commands are executed, the result set is built
up. A final <command>RETURN</command>, which should have no
argument, causes control to exit the function (or you can just
let control reach the end of the function).
</para>
<para>
<command>RETURN QUERY</command> has a variant
<command>RETURN QUERY EXECUTE</command>, which specifies the
query to be executed dynamically. Parameter expressions can
be inserted into the computed query string via <literal>USING</literal>,
in just the same way as in the <command>EXECUTE</command> command.
</para>
<para>
If you declared the function with output parameters, write just
<command>RETURN NEXT</command> with no expression. On each
execution, the current values of the output parameter
variable(s) will be saved for eventual return as a row of the
result. Note that you must declare the function