 |
| |  |
Home In This Issue Email a Friend EasyPrint
 | |
|
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.
[ Next ]
|
|
-- Advertisement --
AUTOMATE LOTUS NOTES USER ID MANAGEMENT
ID Manager 4.5 from HELP Software provides a new level of automaton for managing Lotus Notes IDs. ID Manager lets Lotus Notes administrators get out of the business of creating and managing user IDs. Use our ROI calculator to see how quickly ID Manager will pay for itself.
Learn more about HELP Software products |
-- Advertisement --
How good are your Notes Reports?
Integra for Notes provides high value reporting and data analysis from Lotus Notes databases using Microsoft Word, Excel and PDF files.
- Enhance traditional static reports with Excel data analysis, pivot tables, macros
- Report from any Lotus Notes databases without changes to database design
- Runs reports through a Lotus Notes client and a web browser
- Enables Report scheduling or distribution by e-mail, printing or storing in a Notes database
- Allows use of LotusScript for advanced data manipulation
Enables self service reporting capabilities to end-users.
Click For More Info. |
Copyright © 1998-2008, ZATZ Publishing. All rights reserved worldwide.
|