Search DominoPower's 11,964 Lotus-related article archive 
Home
EasyPrint
News details Click here for the RSS feed's XML code. This is not a browser URL.
Articles-only Click here for the RSS feed's XML code. This is not a browser URL.
Twitter Feed Click here for the Twitter feed.
SIMPLIFYING ADVANCED PROGRAMMING TECHNIQUES
Using recursion in LotusScript
By Greg White

LotusScript is a powerful programming language that gives developers the ability to utilize advanced programming techniques. One of these techniques is recursion. Many developers hesitate to use recursion, preferring to use techniques with which they're already familiar. But using recursive functions can greatly simplify your code, if you implement them correctly.

What is recursion?
Simply defined, a recursive function is one that calls itself. While that definition may be simple, writing a recursive function for the first time can be difficult because it requires a different method of thinking.

Most functions are coded in a fairly straightforward fashion -- you progress from point A, to point B, and end at point C. You may have loops and If Then statements, but you can easily visualize the function in a linear fashion. Recursive functions, however, take an inside-out approach to arriving at an answer. Consider the recursive function below, which calculates the factorial of a number. The factorial of a number is arrived at by multiplying it by all smaller integers. So, the factorial of 3 (denoted as 3!) is equal to 3 * 2 * 1, or 6.

Function factorial (i as integer) as long
If i = 1 then
Factorial = 1
Else
Factorial = i * Factorial(i -1)
End If

End Function

Stepping through a recursive function
Let's step through this function to see how recursion works. We'll assume that you've called the factorial function with a value of 3 as the integer parameter.

You can easily see that since 3 is not equal to 1, the line of code you're concerned with here is:

Factorial = i * Factorial(i - 1)

In English, this line would roughly translate into "The factorial of 3 is equal to 3 times the factorial of 2." So, in other words, the factorial of 3 is equal to 3 times some as of yet unknown value.

Let's figure out that unknown value. If you were to call the factorial function with a value of 2 (which is exactly what this function is doing), you'd see that the factorial of 2 is equal to 2 times the factorial of 1.

You're almost there! Call the factorial function with a value of 1, and the answer is simple: The factorial of 1 is 1. Now what?

Let's take a look at the results we've come up with so far:

3! = 3 * 2!
2! = 2 * 1!
1! = 1

Working from the bottom up, you can easily see that the factorial of 3, or 3!, is equal to 6.


1  ·  2  ·  3  ·  4  ·  Next »
Other articles you might like
Home > Lotus Technologies > LotusScript (64 articles)
   When the debugger won't debug hidden code that isn't hidden
   What to do if the LotusScript debugger won't single-step over code
   Little known traps about Lotus Notes fields
Get Weekly Email Updates
Subscribe to our regular weekly email newsletter. It's packed with tips, reviews, deep analysis, and the latest news.
 
Recent DominoPower Articles
It's time for Lotus to double-down on Linux and open source
Back to basics with Notes: what you can do now
Back to basics with Notes: what are the basics?
Back to basics with Notes
Smart Upgrade bug in Domino 8.5.1 (and some work-around tips)
Hands-on with XPages and the Lotus Solutions Catalog
A tool for migrating Exchange to Domino
Latest Lotus Headlines
IBM Lotus Quickr for WebSphere Portal 8.5 - Various Notes on Administration
Attachment Viewer on Steroids
Admin tip: Predictor, a pregnancy test for your Domino server
Lotus Traveler Webinar
Domino 8.5.1 mail routing problem solved
A small tip for upgrading your XPages apps from 8.5.1 to 8.5.2
What is in your plugin_customization.ini?
>> Read all the news
More from the ZATZ journals
Computing Unplugged: Smartphone smarts for a mobile world
David Gewirtz Online: CNN commentary and analysis
OutlookPower: The strange case of Outlook losing notes and requiring passwords
-- Advertisement --

Learn Notes and Domino 8 at your place and pace!
Learn Notes and Domino in your office and/or home! TLCC's highly acclaimed distance learning courses for users, developers, and admins will enhance your career and your resume.

The many included activities and demos will make you a pro! Expert instructor help is a click away.

Click here to try a FREE demo course!!

-- Advertisement --

Get and stay on top of all your Lotus Notes scheduled agents
With Agent Auditor you can easily gather detailed information on all of your agents or be notified when selected scheduled agents aren't running.

In addition to reporting most agent properties, Agent Auditor provides a tremendous amount of incredibly valuable information about your Notes scheduled agents.

Plus, source code for all agents (LotusScript, Formula, and Java).

Tap here and keep tabs on your agents.

ZATZ Home  ·  News  ·  Back Issues  ·  Credits/Trademarks ·  Link To Us
Copyright © 1998-2010, ZATZ Publishing. All rights reserved worldwide.
Editor's Login