asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search





Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Recursive Functions
By Muhammad Umair Siddique
Rating: 3.1 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Introduction

    A function that calls itself repeatedly, satisfying some condition is called a Recursive Function. Using recursion, we split a complex problem into its single simplest case. The recursive function only knows how to solve that simplest case. You'll see the difference between solving a problem iteratively and recursively later.

    Warning!

    Beware! Use the recursion technique carefully, if you fail to handle it appropriately you can end up with a never-ending ASP application.

    Same Old Factorial problem

    Almost for every language, the beginners learning the concept of recursion are presented with the same old Factorial problem. I'll follow the traditional path, because I feel that it's the easiest way to understand the basic concept of recursion.

    Understanding the problem

    In mathematics, the factorial of a positive integer n, written as n! is represented by the following formula:

    n! = n . (n-1) . (n-2) . (n-3) . Š. . 1

    In plain english, factorial of a number is the product of that number with a number 1 less than that number, this multiplication process continues until the number which is being multiplied, eventually becomes equal to 1.

    Iterative Solution

    In the following function the value of 5! is calculated iteratively:

    
    <%
    Response.Write iterativeFactorial (5)
    
    Function iterativeFactorial ( intNumber )
    	Dim intCount, intFactorial
    
    	intFactorial = 1
    	For intCount = intNumber To 1 Step -1
    		intFactorial = intFactorial * intCount
    	Next
    
    	iterativeFactorial = intFactorial
    End Function
    %>
    
    

    Recursive Solution

    The following function represents the recursive solution for the factorial problem.

    
    
    <%
    Response.Write recursiveFactorial (5)
    
    Function recursiveFactorial ( intNumber )
    	If intNumber <= 1 Then
    		recursiveFactorial = 1
    Else
    	recursiveFactorial = intNumber * recursiveFactorial ( intNumber - 1 )
    	End If
    End Function
    %>
    
    

    The working of the above function recursiveFactorial is simple. If the integer argument "intNumber" is less than or equal to one, then the function returns 1, else the return value of the function is the product of intNumber with the value returned by the same function for integer argument "intNumber-1".

    Answer of the above problem:

    5! = 5 . (5-1) . (5-2) . (5-3) . (5-4)

    Simplifying:

    5! = 5 . 4 . 3 . 2. 1 = 120

    Example: Folder Tree

    The following example will display the tree structure for a specified folder.

    
    

    <% ' Displays the Tree structure, replaces vbTab character with 3 HTML spaces Response.Write Replace (DisplayFolderTree ("c:\windows", 0), vbTab, "   ") Function DisplayFolderTree ( strFolder, intTreeLevel ) Dim objFileSystem, objFolder, objSubFolders, objTempFolder Set objFileSystem = CreateObject("Scripting.FileSystemObject") ' Get the name of current folder Set objFolder = objFileSystem.GetFolder ( strFolder ) ' Get the list of subfolders Set objSubFolders = objFolder.SubFolders ' Adds the name of current folder with proper indentation ' intTreeLevel is used only for indenting folder name according to its pos ition DisplayFolderTree = DisplayFolderTree & String ( intTreeLevel, vbTab) & "•" DisplayFolderTree = DisplayFolderTree & vbTab & objFolder.Name & "<br>" ' Recursion takes place here ' If any "subfolders exist", the function is repeated for them too! If objSubFolders.Count > 0 Then For Each objTempFolder in objSubFolders strArgument = strFolder & "\" & objTempFolder.Name DisplayFolderTree = DisplayFolderTree & DisplayFolderTree ( strArgument, intTreeLevel+1 ) Next End If ' When the control reaches HERE! This means your function has ended ' Now no more recursive calls to the function will take place End Function %>

    Conclusion

    The recursive functions solve the problem in a method much identical to their real-life solutions. In the example above, we simply display the name of current folder. Then we obtain a list of subfolders and repeat the function for each of them.

    While using iterative functions we know about the number of iterations, ie. how many times a loop will be executed, in advance.

    In the factorial problem, recursion is not necessary because we know that a loop will be executed until the value of the number becomes 1. But, in Folder Tree example, we never know how many subfolders may be present in a folder and how many iterations will be required. For this purpose, the most suitable solution is to call the function recursively to display information about a folder and its subfolders.

    About the Author
    If you have any questions about this article, e-mail them to Muhammad Umair Siddique

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Aug 7, 2002 - Using MySQL in the Win32 Environment
    Developers who don't want to spend a lot of money on SQL Server and who want a database that's more robust than Access may find MySQL to be a pleasant alternative. This introductory article covers the bare essentials for getting MySQL installed and running in the Win32 environment.
    [Read This Article]  [Top]
    Jul 17, 2002 - Software Development: Steps To Better Ensure Success
    There is never a guarantee of project success when endeavoring to build a sophisticated application. However, there are established steps to follow that will ensure a clear, concise scope, support for the team involved, and a solid opportunity for successful deployment.
    [Read This Article]  [Top]
    Jul 15, 2002 - Securing SQL Server for Web Applications
    If your SQL Server is exposed to the Internet, then hackers are probing it. This article shows how to secure a SQL Server database that's being used with a Web application
    [Read This Article]  [Top]
    Jul 1, 2002 - Protecting Your Web Application Against Dangerous Requests
    Enrico Di Cesare provides a solution for hiding and securing querystring values that pass through a url.
    [Read This Article]  [Top]
    Apr 2, 2002 - Object-Oriented Programming for VBScripters
    Feel intimidated by .NET? This article by Rob Chartier is designed to ease any level VBScripter (ASP) into .NET by clarifying some OOP concepts.
    [Read This Article]  [Top]
    Mar 27, 2002 - A Best Practice for Using ADO Objects
    A few members of the 15 Seconds discussion list talk about the proper way to use methods in order to prevent ADO object errors.
    [Read This Article]  [Top]
    Jan 2, 2002 - The ASP.NET Page Life Cycle
    Solomon Shaffer explores the life cycle of an ASP.NET page from initialization to unloading. He also explains the various methods to override ASP.NET server-side events.
    [Read This Article]  [Top]
    Dec 19, 2001 - Application Architecture: An N-Tier Approach - Part 2
    Rob Chartier creates a simple portable and reusable address book in .NET to demonstrate the power of N-tier application architecture. Complete source code included!
    [Read This Article]  [Top]
    Oct 23, 2001 - Application Architecture: An N-Tier Approach - Part 1
    Learn about N-tier application architecture and realize that developing with multiple layers produces a flexible and reusable application for distribution to any number of client interfaces.
    [Read This Article]  [Top]
    Oct 23, 2001 - Application Architecture: An N-Tier Approach - Part 1
    Learn about N-tier application architecture and realize that developing with multiple layers produces a flexible and reusable application for distribution to any number of client interfaces.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers