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
Nested Tables versus Index-by Tables The Index-by tables and nested tables are just similar. For e.g.  They have similar structure and their individual elements are accessed in

Semidifference and NOT - SQL In this section first describe the relational difference operator, named MINUS. Example here shows SQL's closest counterpart of that operator.

Write a cursor to open an employee database and fetch the employee record whose age is greater than 45

Components of an Object Type: An object type encapsulates the operations and data. Therefore, you can declare the methods and attributes in an object type specification, but no

Parameter & Keyword Description: function_name: The user-defined function is identifying by that keyword. parameter_name: This identifies the formal parameter that

WHILE-LOOP The WHILE-LOOP statement relates a condition with the series of statements enclosed by the keywords LOOP and END LOOP, as shown: WHILE condition LOOP sequence_of_sta

Project Description: I want to write some SQL statements. The things I need are between pages 5-7. The only problem is that i want it till tomorrow. Skills required is SQL

Boolean Values Only the values TRUE, FALSE, & NULL can be assigned to a Boolean variable. For illustration, given the declaration DECLARE done BOOLEAN; the following statements

Joining in SQL Joining IS_CALLED and IS_ENROLLED_ON in SQL SELECT * FROM IS_CALLED NATURAL JOIN IS_ENROLLED_ON This is an example of an SQL table expression. I have been

Existential Quantification - SQL Existential quantification-stating that something is true of at least one object under consideration-can be expressed by OR(r,c), meaning tha