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
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 Aggregation on Nested Tables Example is the most direct translation of its counterpart in the theory book that can be obtained in SQL but it is so over-elaborate that no

Using EXTEND To enlarge the size of a collection, use EXTEND. This process has 3 forms. The EXTEND appends one null element to a collection. And the EXTEND(n) appends n null e

Explicit Cursors The set of rows returned by the query can include zero, one, or multiple rows, depending on how many rows meet your search criteria. Whenever a query returns

On occasion, some of Brewbean's customers mistakenly leave an item out of a basket already checked out, so they create a new basket containing the missing items. However, they requ

THEO R Y OF PANSPERMIA - Arrhenius (1908) postulated the cosmic panspermia theory that claims that organisms existed throughout the universe, and their spores, etc., could

Define basic operators of relational algebra with an example each

Advantages of PL/SQL The PL/SQL is a high-performance transaction processing, completely portable language that offers the following advantages as shown: 1) Support for SQL

Object Types An object type is a user-defined complex datatype which encapsulates the data structure along with the functions and procedures required to manipulate the data. Th

THEO R Y OF SPECIAL CREATION - Life originated on the earth due to natural events by the super natural power. The biblical story of creation of world within six days was p