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
Data Types in SQL - Interval, Boolean INTERVAL for values denoting, not intervals (!) but durations in time, such as 5 years, 3 days, 2 minutes, and so on. BOOLEAN, con

Running the PL/SQL Wrapper To run the PL/SQL Wrapper, go through the wrap command at your operating system prompt by using the syntax as shown: wrap iname=input_file [oname=

Renaming Columns - SQL SQL has no direct counterpart of RENAME. To derive the table on the right in Figure 4.4 from the table on the left, Tutorial D has IS_CALLED RENAME ( St

Inner Join We have learned how to retrieve data from one table by using SELECT statement. But, as we have learned, normalized relational databases mean the data is spread betw

ROWID and UROWID Internally, every database table has a ROWID pseudo column that stores binary values known as rowids. Each rowid shows the storage address of a row. A physical

to write a heap sort program usin pl-sql

Project Description: I have two types of data sources. One that is a list in SharePoint and another that is an access desktop database. The access desktop database is fairly com

Updating by insertion Syntax : INSERT INTO ENROLMENT VALUES (SID ('S4'), 'Devinder', CID ('C1'));

Data Types in SQL - XML, Array, Row BINARY LARGE OBJECT for arbitrarily large bit strings. XML for XML documents and fragments. ARRAY types for arrays.

Name Resolution   During the compilation, the PL/SQL compiler relates identifiers like the name of a variable with an address or memory location, actual value, or datatype. Th