# Defining your own functions - a few calculators

Did you know that you can define your own functions in Text Blaze? A function is a reusable action that does something, mainly transforming existing values in some way.

Text Blaze already has a bunch of functions pre-configured. Personally I really like text string functions, like contains or concat. Math functions are really useful as well, like round, remainder, or random.

All of these functions work with the formula command and are formatted as functionname(thing_to_do_the_function_to) as follows:

Contains: {=contains("This is my text", "text")}
Concat: {=concat("First string ","and ","second string")}

Round: {=round(12.6)}
Remainder: {=remainder(5,3)}
Random: {=random()}

Being able to define your own function is helpful if you have some sort of math equation you need to do frequently, or if you're changing text in a set way regularly. Creating your own functions, especially for math, lets you create snippets that are essentially your own quick calculators.

To define your own function, you'll use assignment mode and the format of x -> do_something_to_x. Full details are here in our documentation.

By defining these custom functions, I can call the function within my snippet when I need to do what the function does, instead of writing out its formula every single time. Here's some examples of custom functions that I've created that can be used as calculators:

Converting celsius temperature to farenheit
Defining my function: {farenheit=x -> (x*(9/5))+32}

Using my function with a form field as the input:
Enter temperature in Celsius {formtext: default=100; name=celsius}
That temperature is {=farenheit(celsius)} in Farenheit

Converting inches to centimeters
Defining my function: {cm=x -> x*2.54}

Using my function
Enter the number of inches: {formtext: default=12; name=inches}
{=inches} inches is {=cm(inches)} centimeters

Calculating kPa from PSI
Defining my function: {kPa=x -> concat((x*6.9),"kPa")}

Using my function
Enter the tire's PSI: {formtext: default=110; name=PSI}
Result: {=kPa(PSI)}

Text functions are less likely to be used as calculators, but they can still be defined if you plan to manipulate text in complex ways repeatedly in a snippet.

Defining my function: {wonkify=x -> concat(x, " & ", reverse(upper(x)))}

Using my function:
Enter text to wonkify: {formtext: name=regular}
Here's the wonkified version: {=wonkify(regular)}

1 Like

This is what I love about TB, it's almost like a full programming language.

I've been thinking about builing some functions that I use a lot, and I store them all in a special "library" snippet called "Global Functions".

I would then import this Global Functions Snippet into my other snippets and then I would have access to all the functions that I created.

It also means that if I ever have to fix or change a function, I only need to change it in the Global Functions snippet rather than in every snippet.

2 Likes

awesome idea, I love that!

2 Likes

Hi @Andrew_Hall was wondering if there's a way to round, but also have halves like :

1.0 1.5 2.0 2.5 etc

Thanks !

I came up with a really, really circuitous way of doing this. I wonder if one of our engineers has a better suggestion than what I put together here because this feels very inelegant. It also only works with the first 2 decimal places, it won't factor in any additional decimal places (e.g. 123.25 will round to 123.5, but 123.249 won't).

{formtext: default=123.45; name=input}
{if: contains(input, ".")}{firstdecimal=left(split(input, "."), 1)}{seconddecimal=substring(split(input, "."), 2, 1)}{if: firstdecimal=0 or firstdecimal=1}{output=floor(input)}{elseif: firstdecimal=2 and seconddecimal<5}{output=floor(input)}{elseif: firstdecimal=2 and seconddecimal>4}{output=floor(input)+0.5}{elseif: firstdecimal=3 or firstdecimal=4 or firstdecimal=5 or firstdecimal=6}{output=floor(input)+0.5}{elseif: firstdecimal=7 and seconddecimal<5}{output=floor(input)+0.5}{else}{output=ceil(input)}{endif}{else}{output=input}{endif}
Rounded version: {=output; format=.1f}

1 Like

Thanks ! Will try it

It's for half pills, so formula does need to be rounded, but halves often provide precision.

a friend suggested multiply by 2, then round, then divide by 2. Thanks again

that is a much smarter way of going about it!