In the PL/SQL code shown above, the function calc_area is defined so that it returns a number.
In the function body there must be a RETURN statement defining what is returned. In the example above, the RETURN statement is at line 4.
A function name can be very descriptive with up to 32 characters and the function is always created in the schema of the user that creates the function.
As with the procedure, a function can declare any number of values in the declaration section, limited only by the usability of the function. However, unlike a procedure, a function can not be passed variables in mode OUT or INOUT.
A function can only return a datatype and if a function is defined with an
- OUT or
- INOUT
variable, the function will compile but will throw an exception when executed.
ORA-06572: Function <name> has out arguments
An example used earlier in the book converted a temperature in Fahrenheit to Celsius.
This is a perfect example of a function, which takes a value and returns a number.
SQL> create or replace function f2c
2 (n_faren IN number)
3 return number
4 as
5 n_cel number := 0;
6 begin
7 n_cel := (5/9)*(n_faren -32);
8 return n_cel;
9 end;
10 /
Function created.