# function

> 1. Declaring a function

> 2. Declaring an external function

Functions are used to determine test behavior, organize test execution and structure computation. They can be defined within a module or externally; restricted to a component or not. They may have parameters and return a single value.

Related keywords:

- in
- inout
- out
- return
- runs on
- template

#### 1. Declaring a function

### function function_identifier ( [ value |timer |template |port ... ] )[ runs on component_reference ] [ return returned_type ] {statement_block};

- The function keyword introduces the function definition.

- function_identifier is the name used to refer to the function. Must begin with a letter, may contain letters, numbers and underscore characters. According to the Naming convention, the prefix f_ is recommended.

- value indicates a value parameter. The expression begins with an optional keyword indicating the parameter passing method (in, out or inout), continues with a reference to a built-in type (cf. their list) or a referenced type and ends by a parameter identifier.

- timer indicates a timer parameter. The expression begins with an optional keyword indicating the parameter passing method (inout), continues with the keyword timer and ends by a parameter identifier.

- template indicates a template parameter. A template parameter may hold values defined for the value parameter and, in addition, the special value omit, matching symbols (?, * etc.) and templates. The expression begins with an optional keyword indicating the parameter passing method (inout), continues with the keyword template, a reference to a built-in type (cf. their list) or a referenced typeand ends by a parameter identifier.

- port indicates a port parameter. The expression begins with an optional keyword indicating the parameter passing method (inout), continues with the port type identifier and ends by a parameter identifier.

- ... indicates that all kind of parameters (value, timer, template and port) may be repeated. They are separated by comma.

- The optional keywords runs on restrict the function to a component.

- component_reference refers to the component the function is tied to. It is composed of two parts: the module identifier (may be omitted) and the component tpye identifier. The two parts are linked by a dot.

- The optional keyword return indicates that the function returns a value

- returned_type is one of the built-in types (cf. their list) or referenced types determining what kind of a value will be returned

- statement_block may contain both local definitions (of constants, variables and timers) visible only in the defined function and the program part describing the program behavior.

Example

#### 2. Declaring an external function

### external function function_identifier ( [ value |timer |template |port ... ] ) [ return returned_type ] ;

- The external function keywords mean that the function is defined in a module other than TTCN-3 or ASN.1 (for example in a module written in C++).

- function_identifier is the name used to refer to the function. Must begin with a letter, may contain letters, numbers and underscore characters.

- value indicates a value parameter. The expression begins with an optional keyword indicating the parameter passing method (in, out or inout), continues with a reference to a built-in type (cf. their list) or a referenced type and ends by a parameter identifier.

- timer indicates a timer parameter. The expression begins with an optional keyword indicating the parameter passing method (inout), continues with the keyword timer and ends by a parameter identifier.

- template indicates a template parameter. A template parameter may hold values defined for the value parameter and, in addition, the special value omit, matching symbols (?, * etc.) and templates. The expression begins with an optional keyword indicating the parameter passing method (inout), continues with the keyword template, a reference to a built-in type (cf. their list) or a referenced typeand ends by a parameter identifier.

- port indicates a port parameter. The expression begins with an optional keyword indicating the parameter passing method (inout), continues with the port type identifier and ends by a parameter identifier.

- ... indicates that all kind of parameters (value, timer, template and port) may be repeated. They are separated by comma.

- The optional keyword return indicates that the function returns a value

- returned_type is one of the built-in types (cf. their list) or referenced types determining what kind of a value will be returned

Example

Example 1a:

function f_MyF_1 (integer pl_1, boolean pl_2) {};

The function f_MyF_1 has two input parameters (the integer pl_1 and the Boolean pl_2). No value is returned and the function is not restricted to a given component. The statement block is empty.

Example 1b:

function f_MyF_2() return integer { return 28 };

The function f_MyF_2 has no input parameters but an integer value is returned. The function is not restricted to a given component. The statement block is always returns the integer value 28.

Example 1c:

function f_MyF_3() runs on MyCompType_CT {};

The function f_MyF_3 has neither input parameters nor returns any value. The function is not restricted to the component MyCompType_CT. The statement block is empty.

Example 2:

external function f_Utifraan() return float;

The function called f_Utifraan MonConst is defined in an external module, e.g. in a module written in C++. It has no input parameters but returns a floating point value.

BNF definition of function