CHAPTER 3Functions

q defines the following function types:

  • operators and primitive functions
  • q-SQL functions, as described in the previous chapter, e.g. select, update, upsert, insert
  • user-defined functions
  • derived functions, with the use of adverbs.

All predefined q functions can be used in both infix or prefix notations, whereas user-defined functions must be called using a prefix notation. For example, the predefined dyadic operator (function) + can be written as follows:

image

A q function is delimited with open and closed curly brackets { and }. The optional arguments of the function are defined in square brackets separated with semicolon, i.e. [arg1;arg2;...;argn] declared at the beginning of the function. Function parameters x, y and z are implicitly defined. Functions can have a name with the assignment operator : as for variables.

A function returning the square of a number is defined as:

image

Hence,

image

returns

image

Alternatively, we can write:

image

to get the same output. Using the square brackets ...

Get Machine Learning and Big Data with kdb+/q now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.