Example
Write a reusable price function
A Function is logic that other logic calls: inputs in, outputs out, no API or button of its own. This one takes a subtotal, a tax rate and a discount, refuses a tax rate outside 0 to 1, and returns the total rounded to cents.
You will build
- The Price with tax function, belonging to no system
- A check that refuses a wrong tax rate
- A quick test you can run any time
Part 1: Create the function
- 1.
Open Logic in the sidebar and click New logic. Choose Function. - 2.
Fill in: - System
- No system (reusable anywhere)
- Name
- Price with tax
- 3.
Under What it takes, click Add input three times: - subtotal
- Number
- taxRate
- Number
- discount
- Number
- 4.
Under What it gives back, click Add return value: - total
- Number
- 5.
Click Create and open. The editor opens with a Function Entry start and a Return step.
Part 2: Refuse a wrong tax rate
- 1.
Add Validate Range and connect Function Entry taxRateinto itsvalue. In Details: Field nametaxRate, Min0, Max1. - 2.
Add a Branch Tax rate allowed?. Connect the Function Entry arrow into it and the validationvalidintocondition. - 3.
From False: add Throw Error and connect the validation messageinto itsmessage.A Function has no status codes. Throwing stops the caller’s run with the message, so a wrong rate never becomes a wrong price.
Part 3: Work out the total
- 1.
Add Multiply: Afromsubtotal,BfromtaxRate. That is the tax. - 2.
Add Add: Afromsubtotal,Bfrom the Multiplyresult. - 3.
Add Subtract: Afrom the Addresult,Bfromdiscount. - 4.
Add Round: valuefrom the Subtractresult,decimals=2. - 5.
Connect Branch True to the Return step and connect the Round resultintototal.
Check:
Part 4: Test it
- 1.
Click Test in the toolbar. Set subtotal=100,taxRate=0.12,discount=10and run.The True path lights up and totalis 102. - 2.
Set taxRate=12and run again.The False path lights up and the run stops with “taxRate must be ≤ 1”. - 3.
Open the Code tab to see the same function in TypeScript, Python, C#, Java, Go, Kotlin, Swift, Dart, PHP, Ruby or Rust.
