Search /
Docfinder:
Advanced search  |  Help  |  Site map
RESEARCH CENTERS
SITE RESOURCES
Click for Layer 8! No, really, click NOW!
Networking for Small Business
TODAY'S NEWS
First iPhone worm spreads Rick Astley wallpaper
Four reasons to buy (and one reason to avoid) the Droid
Stimulus for tech and telecom $3B, but jobs still guesswork
Cisco MARS shuts out new third-party security devices
Verizon Droid buzz muted in Boston
Week in Google news: Google Dashboard, Droid fever, focus on e-commerce
Cloud computing, virtualization proponents getting antsy
Data center start-up offers energy saving software
Vendors scrambling to fix bug in Net's security
Judge dismisses lawsuit challenging Gartner's Magic Quadrant
Boston Celtics clamp down on spam
Cloud computing inevitable? Not so fast, educator says
Blue Coat slashes staff, buys S7 services company
Apple seeks new sheriff to lock up iPhones
Applications /

Recursive call or loop

Related linksToday's breaking news
Send to a friendFeedback

Sign up to receive this and other networking newsletters in your inbox.

It should come as no surprise to you that you can write any recursive call with a " for " or a " while " loop. So why would you consider writing code that makes recursive calls?

The first answer to this question usually comes in the form of a statement of surprise from many programmers I talk to. " I did not even know there was any other way to do this . . . "

The first rule to consider is that if a problem can be solved effectively and quickly using loop constructs then that should be your first choice. For most algorithms loops are easier and quicker to write and are a natural component of your programming arsenal. Before you start thinking about moving a loop to a recursive call rather explore tightening the loop, make it more efficient.

Recursive method calls or algorithms, however, often offer us a natural and elegant way of dealing with a complex problem, and this is the reason I brought the subject up over the past few weeks. We are going to look at some data structures that can be elegantly manipulated with recursive algorithms.

You will find that many algorithms are inherently recursive and those may be better coded with recursion than loops. Keeping the method and the size of the data structure that is being worked on small is very important. This is because recursive calls tend to impact the call stack, especially when the dataset explodes.

A good example (or a bad example depending on how you look at) of such a case is processing the Fibonacci series:

fibonacci(0) = 0

fibonacci(1) = 1

fibonacci(n) = fibonacci(n-1) + fibonacci(n - 2)

Processing Fibonacci 20 results in 21,891 calls, but processing Fibonacci 30 quickly racks up more than seven million recursive calls - clearly if you keep going you'll send the call stack into oblivion.

Next issue I'll provide a list of specific tips for using recursion as we prepare to get our hands a little green pruning trees and similar data structures.

 

RELATED LINKS

Jeffrey Shapiro is a software development expert and well-known author. He has written several books, which include the widely acclaimed Computer Telephony Strategies, by Hungry Minds, and Visual Basic .NET Headstart by Osborne/McGraw-Hill. You can reach him by e-mail at java-pro@mcity.org.


NWFusion offers more than 40 FREE technology-specific email newsletters in key network technology areas such as NSM, VPNs, Convergence, Security and more.
Click here to sign up!
New Event - WANs: Optimizing Your Network Now.
Hear from the experts about the innovations that are already starting to shake up the WAN world. Free Network World Technology Tour and Expo in Dallas, San Francisco, Washington DC, and New York.
Attend FREE
Your FREE Network World subscription will also include breaking news and information on wireless, storage, infrastructure, carriers and SPs, enterprise applications, videoconferencing, plus product reviews, technology insiders, management surveys and technology updates - GET IT NOW.
* HOME    * RESEARCH CENTERS     * NEWS     * EVENTS

Contact us | Terms of Service/Privacy | How to Advertise
Reprints and links | Partnerships | Subscribe to NW
About Network World, Inc.

Copyright, 1994-2006 Network World, Inc. All rights reserved.