You are on page 1of 2

Recursive Algorithms

Author: Ulysses R. Gotera


Recursive algorithms in an application development are functions that call themselves repeatedly until they reach a satisfied condition. It helps in solving complex iterative problems into a single simple case. One of its advantages of recursive algorithms is you do not need to know in advance the number of times you have to repeat a procedure. You just have to exercise great care in creating one as it may end up in an infinite loop. Recursive algorithms are also shorter to write and elegant to both humans and machines. It gives your code a performance boost as ordinary iterative procedures are written longer and introduce complexity to you and your CPU.

Factorial as a Mathematical Example


A classic mathematical example is a factorial of a positive integer N, defined as follows: N! = N*(N-1)*(N-2)2*1 Allow me to illustrate as follows: 5! = 5 * 4 * 3 *2 * 1 Ans: 120 Here its a recursive pseudocode:
Function factorial is: Input: integer n such that n >= 0 Output: [n x (n-1) x (n-2) x x 1] 1. If n is 0, return 1 2. Otherwise, return [ n x factorial (n-1)]

Applied in C#:
private int intFactorial (int aIntNumber) { if (aIntNumber == 0) return 1; else return aIntNumber * intFactorial (aIntNumber--); }

A Real World Software Development Application Example


Our CIRF (Change Implementation Request Form) consists of the following reference number pattern:

<SSSS><YY><CCCCC><UU>

Recursive Algorithms
Author: Ulysses R. Gotera

Where: SSSS YY CCCCC UU stands for a given system code, such as Business Object Reports System (BORS). the last two numbers in year (ie 2011, the last two number is 11) th th the CIRF count. A value of 10 means the 10 CIRF for the year, a value of 800 as the 800 CIRF for the year and so on. manually assigned by the user. A 00 means that the CIRF is for production while a manually assigned 01 up to 99 is the number of times an application system was submitted for UAT (User Acceptance Testing).

We would require that the YY and CCCCC are system generated. tblCounter is created in our database to store the current year and the last given value for CCCCC. In generating our reference number, we need to check for a duplicate in another table that holds CIRF data (tblCIRF) and regenerate a new one to ensure uniqueness. This is also to address split second data entry where two or more users can input new CIRF records. The pseudocode for this requirement is as follows:
Function GenerateRefNum is Input: SSSS and UU Output: <SSSS><YY><CCCCC><UU> 1. Get the year and count in tblCounter. 2. Get the machines (PC) year and compare it with tblCounters year. 3. If year is different then replace the value year with that of the machine and update the year and reset the counter to 1 of tblCounter, else proceed to 4. 4. Increment by 1 and update tblCounters counter field. 5. Concatenate <SSSS><YY><CCCCC><UU>. 6. Search in tblCIRF <SSSS><YY><CCCCC><UU> in its RefNum field. 7. If found then call GenerateRefNum (<SSSS>,<UU>) 8. Otherwise, return <SSSS><YY><CCCCC><UU>

I will shorten its implementation on C#, showing only numbers 7 and 8 of the pseudocode.
private string fnStrGenRefNum (string aStrSysCode, int aIntUsrCnt) { //Your data reader here reads tblCIRF with a Select //statement that looks for the existence of your newly created Reference Number. if (dtaReader.Read()) { strRefNum = fnStrGenRefNum (aStrSysCode, aIntUsrCnt); } else return strRefNum; }

The above algorithm expressed in C# shows how you can apply recursion on your program. Learning to apply recursive algorithm gives a developer a divide-and-conquer approach technique and creates a more elegant code that is easily digested by your CPU and can effortlessly understood by you and other programmers as well. Practice writing recursive algorithms today! Note: In real practice the code above was done in a stored procedure and a tblCounter was not present. The purpose of the sample above is to give you the idea in using a recursive function.

Page 2 of 2

You might also like