### Using user-defined functions

User-defined functions are used in scripts the same way built-in functions such as mktime() are. Simply use the function in your script, using the script name you entered in the script editor as the function name and passing the appropriate parameters. The following examples show how user-defined functions are created and used.

#### Example 1

The following example shows a function script that is used to generate random numbers between 0 and 1. The function requires that a long integer field named seed be defined as a system variable. The following constants have also been defined using the Constant Definition window:

MODULUS = 2147483647

MULTIPLIER = 16807

QUOTIENT = 127773

REMAINDER = 2836

No parameters are required for the function.

The following script, named random, defines the random() function.

```function returns currency random_number;

local long temp, low, hi, test;

{Keep the current seed. This will be used to get the next random number in the stream.}
temp = seed of globals;
{Generate the next seed.}
hi = seed of globals / QUOTIENT;
low = seed of globals % QUOTIENT;
test = (MULTIPLIER * low) - (REMAINDER * hi);
if test > 0 then
seed of globals = test;
else
seed of globals = test + MODULUS;
end if;
random_number = temp * (1.0) / MODULUS;

```

This script uses the random() function.

```local currency result;

seed of globals = 1;
result = random();

```

#### Example 2

This example shows a function script named Time_AddSeconds that is used to add seconds to a time value. The Time_AddSeconds() function takes a time value and an integer as parameters, and returns a time value.

```function returns time result_time;
in time original_time;

local integer t_hour, t_minute, t_second;

error "Add amount isn't in the valid range. It must be between 0 and 59";
else
{Get the hours, minutes and seconds from the original_time.}
t_hour = hour(original_time);
t_minute = minute(original_time);
t_second = second(original_time);
{Add the seconds to the time.}
if t_second + add_amount > 59 then
t_second = (t_second + add_amount) % 60;
{Need to increment the minutes.}
if t_minute + 1 > 59 then
t_minute = (t_minute + 1) % 60;
{Need to increment the hours.}
if t_hour + 1 > 23 then
t_hour = 0;
else
t_hour = t_hour + 1;
end if;
else
t_minute = t_minute + 1;
end if;
else
end if;
result_time = mktime(t_hour, t_minute, t_second);
end if;

```

The following script uses the Time_AddSeconds() function to add 30 seconds to the current system time.

```local time future_time;