RabbinatixDocs
Sign inGet started

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

    Open Logic in the sidebar and click New logic. Choose Function.

  2. 2.

    Fill in:

    System
    No system (reusable anywhere)
    Name
    Price with tax
  3. 3.

    Under What it takes, click Add input three times:

    subtotal
    Number
    taxRate
    Number
    discount
    Number
  4. 4.

    Under What it gives back, click Add return value:

    total
    Number
  5. 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. 1.

    Add Validate Range and connect Function Entry taxRate into its value. In Details: Field name taxRate, Min 0, Max 1.

  2. 2.

    Add a Branch Tax rate allowed?. Connect the Function Entry arrow into it and the validation valid into condition.

  3. 3.

    From False: add Throw Error and connect the validation message into its message.

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

    Add Multiply: A from subtotal, B from taxRate. That is the tax.

  2. 2.

    Add Add: A from subtotal, B from the Multiply result.

  3. 3.

    Add Subtract: A from the Add result, B from discount.

  4. 4.

    Add Round: value from the Subtract result, decimals = 2.

  5. 5.

    Connect Branch True to the Return step and connect the Round result into total.

Check: The Problems tab is empty, and the Text steps tab reads: total = round(subtotal + subtotal × taxRate − discount, 2).

Part 4: Test it

  1. 1.

    Click Test in the toolbar. Set subtotal = 100, taxRate = 0.12, discount = 10 and run.

    The True path lights up and total is 102.

  2. 2.

    Set taxRate = 12 and run again.

    The False path lights up and the run stops with “taxRate must be ≤ 1”.

  3. 3.

    Open the Code tab to see the same function in TypeScript, Python, C#, Java, Go, Kotlin, Swift, Dart, PHP, Ruby or Rust.