Function Statements
Function Statements
Function statements define reusable custom functions that can be called throughout your Trilogy scripts.
Syntax
function: "def" IDENTIFIER "(" function_binding_list ")" "->" expr
function_binding_item: IDENTIFIER function_binding_type? function_binding_default?
function_binding_type: ":" data_type
function_binding_default: "=" expr
Basic Function
Define a simple function:
def double(x) -> x * 2;
With Type Hints
Specify parameter types:
def calculate_tax(amount: float, rate: float) -> amount * rate;
With Default Values
Provide default parameter values:
def apply_discount(price, discount = 0.1) -> price * (1 - discount);
Calling Functions
Use @ prefix to call custom functions:
def format_currency(amount) -> concat('$', cast(round(amount, 2) as string));
select
order.id,
@format_currency(order.total) -> formatted_total
;
Complex Functions
Functions can use any expression:
def categorize_amount(amount) ->
case
when amount > 1000 then 'high'
when amount > 100 then 'medium'
else 'low'
end;
Use Cases
- Reusable business logic
- Standardized calculations
- Code organization
- DRY principle
Example
# Define helper functions
def margin(revenue, cost) -> (revenue - cost) / revenue;
def growth_rate(current, previous) -> (current - previous) / previous * 100;
import order as order;
select
order.product_id,
sum(order.revenue) -> total_revenue,
sum(order.cost) -> total_cost,
@margin(total_revenue, total_cost) -> profit_margin
;
