Skip to content

G-7420: Always make the RETURN statement the last statement of your function.

Major

Maintainability

Reason

The reader expects the return statement to be the last statement of a function.

Example (bad)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
create or replace package body my_package is
   function my_function(
      in_from in pls_integer
     ,in_to   in pls_integer
   ) return pls_integer
      deterministic
   is
      l_ret pls_integer;
   begin
      l_ret := in_from;
      <<for_loop>>
      for i in in_from..in_to
      loop
         l_ret := l_ret + i;
         if i = in_to then
            return l_ret;
         end if;
      end loop for_loop;
   end my_function;
end my_package;
/

Example (good)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
create or replace package body my_package is
   function my_function(
      in_from in pls_integer
     ,in_to   in pls_integer
   ) return pls_integer
      deterministic
   is
      l_ret pls_integer;
   begin
      l_ret := in_from;
      <<for_loop>>
      for i in in_from..in_to
      loop
         l_ret := l_ret + i;
      end loop for_loop;
      return l_ret;
   end my_function;
end my_package;
/