Product SiteDocumentation Site

8.8. Builtin Functions

The syntax for function calls is similar to many other languages.
Like in C++ (namespaces) or Java (packages, classes), functions live in namespaces that are URIs.
Although it is possible to fully write the name of a function, namespace included, it can be cumbersome. Hence, for convenience, a namespace can be associated with a prefix that acts as a shortcut.
JSONiq supports three sorts of functions:
For now, we only introduce how to call builtin functions -- these are the simplest, since they do not need any prefix or explicit namespace.
Example 8.17. A builtin function call.
keys({ "foo" : "bar", "bar" : "foo" }),
concat("foo", "bar")
Results:
"foo"
"bar"
"foobar"

Some builtin functions perform aggregation and are particularly convenient:
Example 8.18. A builtin function call.
sum(1 to 100),
avg(1 to 100),
count( (1 to 100)[ $$ mod 5 eq 0 ] )
Results:
5050
50.5
20

Remember that JSONiq is a strongly typed language. Functions have signatures, for example sum() expects a sequence of numbers. An error is raised if the actual types do not match the expected types.
Also, calling a function with two parameters is different from calling a function with one parameter that is a sequence with two items. For the latter, extra parentheses must be added to make sure that the sequence is taken as a single parameter.
Example 8.19. Calling a function with a sequence.
count((1, 2, 3, 4))
Results:
4