Functions

FTD supports functions which users can create to execute their own logic in their ftd documents. It also provides various built-in-functions which users can use anywhere in their ftd 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>
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

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
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 ftd.

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)
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
Output
1

Support fastn!

Enjoying fastn? Please consider giving us a star ⭐️ on GitHub to show your support!

Getting Help

Have a question or need help?

Visit our GitHub Q&A discussion to get answers and subscribe to it to stay tuned.

Join our Discord channel and share your thoughts, suggestion, question etc.

Connect with our community!

Found an issue?

If you find some issue, please visit our GitHub issues to tell us about it.

Join us

We welcome you to join our Discord community today.

We are trying to create the language for human beings and we do not believe it would be possible without your support. We would love to hear from you.
Copyright Β© 2023 - FifthTry.com