functionDefinition

Defines a named function with typed parameters and an optional return type. Functions may be fluent (usable via dot notation), external (implemented by the host environment), or defined by a CQL expression body.

Examples

Simple function with parameter and return type

define function "Age At"(asOf DateTime) returns Integer:
  AgeInYearsAt(asOf)

Fluent function callable via dot notation

define fluent function "ToInterval"(period FHIR.Period) returns Interval<DateTime>:
  Interval[period."start".value, period."end".value]

External function (implemented by host)

define function "LogMessage"(msg String) returns String: external

Public multi-parameter function

define public function "CalculateBMI"(weight Decimal, height Decimal) returns Decimal:
  weight / (height * height)
Railroad Diagram
100%
define accessModifier fluentModifier function identifierOrFunctionIdentifier ( operandDefinition , ) returns typeSpecifier : functionBody external

scroll to zoom · drag to pan · click green rules or blue tokens to navigate

EBNF

functionDefinition
  : 'define' accessModifier? fluentModifier? 'function' identifierOrFunctionIdentifier
    '(' (operandDefinition (',' operandDefinition)*)? ')'
    ('returns' typeSpecifier)?
    ':' (functionBody | 'external')
  ;

Used In