Want to try fastn for your company's website?
Book a Demo

Functions

fastn supports functions which users can create to execute their own logic in their fastn documents. It also provides various built-in-functions which users can use anywhere in their fastn document.

How to create functions?

To create a function, you need to follow the function declaration syntax which is as follows:
Function Declaration syntax
-- <return-type> <function-name>(<arg-1-name>, <arg-2-name>, ...):
<arg-1-type> <arg-1-name>: <optional-default-value>
<arg-2-type> <arg-2-name>: <optional-default-value>
...

<function-body>
Lang:
ftd
Using the above declaration syntax, a simple add() function is defined below which takes two integer as arguments and returns the added value.
Sample add() function
-- integer add(a, b):
integer a:
integer b:

a + b
Lang:
ftd

How to use your functions ?

Once functions have been defined, you can use these functions by invoking it by using $.
Sample code using add() function
Input
-- integer add(a, b):
integer a:
integer b:

a + b

-- ftd.column:
spacing.fixed.px: 10
color: $inherited.colors.text

-- ftd.text: Adding 35 and 83

-- ftd.integer: $add(a=35, b=83)

-- end: ftd.column
Lang:
ftd
Output
Adding 35 and 83
118

Some frequently used functions

Below mentioned are some of the most frequently used functions which can be created as per the requirement and are not part of fastn.

Clamp

Clamp functions are used to limit your given value between certain range.

Regular Clamp

This function will clamp the value between 0 and max.

Value will range from [0,max] given max > 0.
Sample code using regular-clamp()
Input
-- integer $num: 0

-- display-integer: $num
$on-click$: $regular-clamp($a = $num, by = 1, max = 6)

-- void regular-clamp(a,by,max):
integer $a:
integer by:
integer max:

a = (a + by) % (max + 1)
Lang:
ftd
Output
0

Clamp with min and max

This function will clamp the value between min and max.

Value will range from [min,max] given max > min.
Sample code using clamp_with_limits()
Input
-- integer $n: 1

-- display-integer: $n
$on-click$: $clamp_with_limits($a = $n, by = 1, min = 1, max = 6)

-- void clamp_with_limits(a,by,min,max):
integer $a:
integer by: 1
integer min: 0
integer max: 5

a = (((a - min) + by) % (max + 1 - min)) + min
Lang:
ftd
Output
1