Functions

`fastn` supports functions which users can create to execute their own logic in their `fastn` documents. It also provides various [`built-in-functions`](/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
--  (, , ...):
 : 
 : 
...


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

Support `fastn`!

Enjoying `fastn`? Please consider giving us a star ⭐️ on [GitHub](https://github.com/fastn-stack/fastn) to show your support!
[⭐️](https://github.com/fastn-stack/fastn)

Getting Help

Have a question or need help? Visit our [GitHub Q&A discussion](https://github.com/fastn-stack/fastn/discussions/categories/q-a) to get answers and subscribe to it to stay tuned. Join our [Discord](https://discord.gg/a7eBUeutWD) channel and share your thoughts, suggestion, question etc. Connect with our [community](/community/)!
[💻️](/community/)

Found an issue?

If you find some issue, please visit our [GitHub issues](https://github.com/fastn-stack/fastn/issues) to tell us about it.

Quick links:

- [Install `fastn`](install/) - [Create `fastn` package](create-fastn-package/) - [Expander Crash Course](expander/) - [Syntax Highlighting in Sublime Text](/sublime/)

Join us

We welcome you to join our [Discord](https://discord.gg/a7eBUeutWD) 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](https://www.fifthtry.com/)