Function - (Oracle Advance)
In Oracle Advance, a function is a stored program that returns a value. It takes zero or more input parameters and processes them to return a single output value. Functions are similar to procedures, but they always return a value.
Syntax
The basic syntax for creating a function in Oracle Advance is as follows:
CREATE [OR REPLACE] FUNCTION function_name
[ (parameter [, parameter]) ]
RETURN return_datatype
IS
[local declarations]
BEGIN
executable statements
[EXCEPTION
exception handlers]
END [function_name];
Here, function_name
is the name of the function, parameter
is a comma-separated list of input parameters, and return_datatype
is the data type of the value returned by the function. The IS
keyword starts the body of the function, and the BEGIN
keyword starts the executable statements.
Example
Here is an example of a function that takes two input parameters and returns their product:
CREATE OR REPLACE FUNCTION product (a NUMBER, b NUMBER)
RETURN NUMBER
IS
BEGIN
RETURN a * b;
END product;
Output
To call the above function, you can use the following SQL statement:
SELECT product(2, 5) FROM dual;
The output of this statement is 10
.
Explanation
In the above example, we have created a function called product
that takes two input parameters a
and b
, both of data type NUMBER
. The function body multiplies the two input values and returns the result. We can call this function using a SQL statement and passing two numeric arguments to it.
Use
Functions in Oracle Advance are used to encapsulate complex logic and return a single value. They can be used in SQL statements, PL/SQL blocks, and other Oracle Advance programs. Functions make it easier to maintain and modify complex logic, as they allow developers to change only the function code without affecting the rest of the application.
Important Points
- A function is a stored program that returns a value.
- Functions take zero or more input parameters and process them to return a single output value.
- Functions are defined using the
CREATE FUNCTION
statement. - A function must have a return data type specified.
- Functions are used to encapsulate complex logic and return a single value.
Summary
In summary, a function in Oracle Advance is a stored program that takes zero or more input parameters and returns a single output value. Functions are defined using the CREATE FUNCTION
statement and can be called from other Oracle Advance programs, including SQL statements and PL/SQL blocks. Functions are used to encapsulate complex logic and make it easier to maintain and modify large applications.