Recursion versus iteration, PL-SQL Programming

Recursion versus Iteration

Dissimilar the iteration, recursion is not crucial to PL/SQL programming. Any problem which can be solved using recursion can be solving using the iteration. Also, the iterative version of the subprogram is typically easier to design than the recursive version. Though, the recursive version is typically simpler, smaller, and hence easier to debug. Compare the functions below that calculate the nth Fibonacci number:

-- recursive version

FUNCTION fib (n POSITIVE) RETURN INTEGER IS

BEGIN

IF (n = 1) OR (n = 2) THEN

RETURN 1;

ELSE

RETURN fib(n - 1) + fib(n - 2);

END IF;

END fib;

-- iterative version

FUNCTION fib (n POSITIVE) RETURN INTEGER IS

pos1 INTEGER := 1;

pos2 INTEGER := 0;

cum INTEGER;

BEGIN

IF (n = 1) OR (n = 2) THEN

RETURN 1;

ELSE

cum := pos1 + pos2;

FOR i IN 3..n LOOP

pos2 := pos1;

pos1 := cum;

cum := pos1 + pos2;

END LOOP;

RETURN cum;

END IF;

END fib;

The recursive version of the Fibonacci is more graceful than the iterative version. Though, the iterative version is more accurate; it runs faster and uses less storage. That is as each recursive call needs an additional time and memory. As the number of recursive calls gets bigger, so does the difference in effectiveness. Still, if you expect the number of recursive calls to be little, you may choose the recursive version for its readability.

Posted Date: 10/5/2012 5:56:34 AM | Location : United States







Related Discussions:- Recursion versus iteration, Assignment Help, Ask Question on Recursion versus iteration, Get Answer, Expert's Help, Recursion versus iteration Discussions

Write discussion on Recursion versus iteration
Your posts are moderated
Related Questions
EXCEPTION_INIT Pragma The pragma EXCEPTION_INIT relates an exception name with an Oracle error number. Which allow you to refer to any internal exception by the name and to wri

Create a view named CustomerAddresses that shows the shipping and billing addresses for each customer in the MyGuitarShop database. This view should return these columns from the

UNNEST operator in SQL The inverse operator of GROUP is UNGROUP. SQL has an operator, UNNEST, that can be used for similar purposes, but its method of invocation is somewhat p

Using a join on 2 tables, select all columns and 10 rows from the 2 tables without the use of a Cartesian product. Query: SELECT * FROM EMPLOYEE1 E JOIN STAFF S ON E.EMP_

Aggregate Operators SQL Supports all of the aggregate operators mentioned in the theory book and many more besides. The syntax, however, involves an unusual trick that SQL cal

Operator Precedence The operations within an expression are completed in a particular order depending on their precedence (priority). The table shows the default order of the op

WHEN or THEN Key Constraints Suppose a table has two columns representing a period of time throughout which the information conveyed by the other columns is recorded as having

Closing a Cursor Variable The CLOSE statement disables the cursor variable. After that, the related result set is undefined. The syntax for the same is as shown below: CLOS

DBMS_PIPE: The Package DBMS_PIPE allows various sessions to communicate over the named pipes. (A pipe is a region of memory used by one of the process to pass information to

Demonstrate your knowledge of PL/SQL programming by writing and thoroughly testing triggers and stored procedures associated with an e-commerce application that provides security l