heitml takes the
World Wide Web
to a higher level!

Introduction
heitml Features
       

Language Guide

   Web Page Templates
   Creating SQL Tables
   Creating FORMs
   Database Access
   Altering SQL Tables
   Updating SQL Tables
   A Sample Program
   User-defined Functions
   Control Statements
   dba Tutorial
   Session Mode
       
Language Reference
The heitml Libraries
Supported Platforms
User Guide
Download
User Registration
Discussion Group
Mailinglist
       
NEW!
heitml 1.2 new features


Send us mail

Contact Info

   

heitml Language Guide
by Keith Oustalet

User-defined Functions
We know there'll be times when you need to define a function for you own purposes. As much as we'd like to provide you with a comprehensive set of Library and Built-In Function, we can't think of everything, so the next best thing is to allow you to create your own.

Functions are defined as follows:

<def FunctionName [Parameter1, Parameter2,...];
   [heitml]
   return expression;
 /def>

The FunctionName is required, of course, but you only have to include as many or as few parameters as needed to complete whatever task you want to perform. Some functions don't require any parameters, but most require one or two. The [heitml] in the above definition means that you can include whatever intermediate statements are necessary to manipulate the passed parameters before arriving at a result. Mostly, however, you'll find that your functions can simply evaluate the parameters in an expression, the result of which is returned or "passed back" to the program.

To illustrate this concept let's first define a couple of simple mathematical functions that require only one parameter. We'll call them sqr and cube because they take a numerical value (real or integer) and raise them to the second and third powers respectively. Here's what they look like:

heitml input:
<def sqr x;
   return x * x;  
 /def>

<let x=4; ? sqr(x)>
<br> <? sqr(5)> 
heitml input:
<def cube x;
   return x * x * x;  
 /def>

<let x = 3; ? cube(x)>
<br> <? cube(4)>
<br> <? cube(sqr(2))>
resulting output:
16
25
resulting output:
27
64
64

As you can see, you can pass parameters to heitml functions any way you like: as pre-defined variables, as constants (sometimes referred to as "immediate" values), or you can even pass another function as an argument (nested functions).

Now we'll go one step further and design a function with two parameters called power, which takes a number x and raises it to the power y:

heitml input:
<def power x y;
  i = 1; a = x;
  while i <y; a = a * x; i = i + 1; /while; 
  return a;  
 /def>

<let x = 3; y = 4; ? power(x,y)>
<br> <? power(2,16)>
resulting output:
81
65536

There are several interesting things we should point out about the above function. First, you'll notice that we couldn't simply evaluate x and y in a mathematical expression and return the result. We had to perform a series of intermediate steps which included

  • initializing a counter variable called i
  • preserving the initial value of x by assigning it to variable a
  • multiplying a by x
  • incrementing the counter variable i
  • and repeating the calculation while i < y
We could also have initialized i to a value of 2 and repeated the calculation while i <= y. Either method would have suited our purposes.

Now let's take a look at some functions that manipulate character strings. Here's an example that returns the leftmost "x" number of characters from a string:

heitml input:
<def left string x;
   return substring(string,0,x);  
 /def>

<let string="1234567890"; x=4; ? left(string,x)>
resulting output:
1234

Notice that we used the heitml Built-in Function substring to do most of the work for us.

And now here's a function that returns "x" number of characters starting from the right side of a string:

heitml input:
<def right string x;
   return
   substring(string,len(string)-x,len(string));  
 /def>

<let string="1234567890"; x=4; ? right(string,x)>
resulting output:
7890

OK, by now you should have an idea of what's required to create your own functions, so go ahead and have fun!


This page was dynamically generated by heitml
© 1996-1997 H.E.I. All Rights Reserved

heitml documentation (c) 1997 H.E.I. GmbH Mannheim, Germany