[ Team LiB ] |
10.2 FunctionsSome languages have a single subprogram type. A Java class has methods and a method may or may not return data. The C language is the same. The only subprogram in C is a function. A C function can return a value or modify the contents of an address value passed as an argument. PL/SQL includes two types of subprograms: procedures and functions. Consider a package to be the encapsulation of operations on some object. That object can be a table, database pipe, host file, tables in a remote database, or many other options. Procedures take actions on the object and modify them. Functions provide a means to acquire status or state information about the object. The package typically behaves as an API that hides an object and provides operations on the object. If the user needs to know, for example, how large the object is, a method must be available. Functions play this role. Functions frequently act as selectors on an object. Consider the function to be the evaluation of an attribute of an object. Functions are not actors, but rather evaluations of a state. You should use nouns to name functions. Figure 10-2 illustrates the components of a function. The key components of a function, to which you write code, are the following:
Figure 10-2. Function.Parameters are optional, but the RETURN part is not. The FUNCTION statement must include a RETURN and a type. The RETURN statement, in the subprogram body, is a key component. A function compilation fails if there is no RETURN statement in the body of the code. A function that follows an execution path that ends before executing a RETURN statement causes a run-time error. A function must have a RETURN statement to compile; it must execute a RETURN during run-time. A frequent oversight is to write an exception handler and not include a RETURN clause. A function that enters an exception handler without a RETURN statement generates a run-time error. The following example is a function that returns a DATE type. The declarative part creates the variable NEXT_DAY part. The program body assigns a value and returns that value.
Functions can include an expression in the RETURN statement. This often means less code in the function body. The following function performs the same logic as the preceding function. In this example, the local variable is not necessary.
If there are no parameters, do not use empty parentheses in the function definition. This rule applies to procedures as well. The user of the function can use the function as a single expression that evaluates to some type. The following code uses the TOMORROW function in an assignment statement. The same procedure uses the function as an argument to DBMS_OUTPUT.
Use functions to increase the readability of the code. The following uses the TOMORROW function in a loop. The code is clear and identifies when the loop stops.
Variables and function calls are always interchangeable provided the function returns the same type as the variable. For example, the TOMORROW function returns a DATE type. Any PL/SQL statement, in which a DATE variable is acceptable, can use the function TOMORROW.
|
[ Team LiB ] |
No comments:
Post a Comment