|
 |
|
|
Functions and Reusable Code
Defining functions is done by including a function statement in your code. Once defined the function stays resident for the entire user session, unless overwritten. Functions support LOCAL variables. Here's an example of a simple function that builds an error message bar:
FUNCTION error_bar(x) DO RETURN "<table border=0 width=300 bgcolor=red>" + "<tr><td>" + "<font color=white>" + x + "</font>"+ "</table>" /RETURN /FUNCTION
Now, throughout your program, to display an error message, one can write DISPLAY error_bar("Please enter full name.") /DISPLAY.
H2O FUNCTIONs support recursion, the ability for FUNCTIONs to call themselves. For example, a factorial function can be defined by writing:
FUNCTION myfactorial(x) LOCALS out DO IF x > 1 THEN out=x*myfactorial(x-1) ELIF INSINTEGER(x) != "TRUE" THEN out="ERROR" ELSE out=1 /IF RETURN out /RETURN /FUNCTION
Functions may be placed in code or in files (library) and EXPANDed. To load code and functions from a file one uses the tag EXPAND. Suppose your code was stored in functions/library.lib. Then you could write: EXPAND FILE="functions/filename.txt" /DISPLAY
Program Forking H2O supports user-session forking. A "FORK" is a new user-session that inherits the current user-session. For example:
<A FORK="/mypopup.html" TARGET="NewWin>Popup Window</A>
A FORK link looks like an HREF link, with the HREF replaced with the word FORK. Activity in forked user sessions has no affect on the parent application until data is passed back to it. |
|