Qore Programming Language Reference Manual  0.9.2
Functions

Function Declarations

A function is declared in Qore by using the keyword sub (for subroutine) as follows.

Function Declaration Syntax
[public] [synchronized] [deprecated] [return_type] sub function_name([param_type] $var_name ...]) {
    statement(s)...
}

or the deprecated alternate syntax with the returns keyword:
[public] [synchronized] [deprecated] sub function_name([param_type] $var_name ...]) returns return_type {
    statement(s)...
}
Function names must be valid Qore identifiers.

When defining a user module, the function definition can be preceded by public, which means that the function variant will be available (imported) in the Program object importing the module. See public for more information.

Function Parameters

Variables listed in parentheses after the function name are the parameters to the function and automatically get local lexical scoping.

Type declarations optionally precede the parameter variable and will restrict any arguments passed to the type declared. The same function can be declared multiple times if each declaration has different parameter types; this is called overloading the function.

Variables passed as function arguments are passed by value by default, unless the caller places a "\" character before an lvalue in the argument list in the function call. In this case the function must have a parameter defined to accept the variable passed by reference. Any changes to the local variable will be reflected in the original variable for variables passed by reference. Also note that it is illegal to pass a local variable by reference in a background expression.

Note
In order to process a variable number of arguments to a function, use implicit argument references ($1) or the argv variable (an automatic local variable); these are automatically instantiated at run time when additional arguments in excess of the declared parameters are passed to the function at run time. No declaration needs to be made in the function signature to use the argv variable.

Function Return Type Declarations

The return type of the function can be given by placing a type declaration before the sub keyword (the older syntax with the returns keyword after the parameter list is still accepted as well).

Note
Parameter and return types are required when the Qore::PO_REQUIRE_TYPES or Qore::PO_REQUIRE_PROTOTYPES parse options are set.
If the return type is missing, it is assumed to be of type any. In case Qore::PO_REQUIRE_TYPES or Qore::PO_REQUIRE_PROTOTYPES parse options are set, and the return type is missing from function's declaration, it is assumed to be of type NOTHING.

Functions use the return statement to provide a return value to the caller.

Simple Example Functions

Here is an example function declaration returning a value:

#!/usr/bin/qore
# function declaration example
%new-style
int sub print_string(string string) {
print("%s\n", string);
return 1;
}

Functions may also be recursive. Here is an example of a recursive Qore function implementing the Fibonacci function:

#!/usr/bin/qore
#
# recursive function example
%new-style
int sub fibonacci(int num) {
if (num == 1)
return 1;
return num * fibonacci(num - 1);
}
Note
Function names are resolved during the second parse pass; therefore functions do not need to be declared before being referenced. This allows an easy definition of 2 or more self-referencing functions.

"Synchronized" Functions

Functions declared with the synchronized keyword will only run in one thread at a time.

If another thread tries to call the function while the function is already being executed, any callers will block until the function has finished executing.

Note that the lock used is recursive, so that a single thread may call the function multiple times safely without fear of a deadlock. The lock used also participates in Qore's deadlock detection framework, therefore if a deadlock is detected, a THREAD-DEADLOCK exception is thrown.

See also
synchronized

"Deprecated" Functions

Functions declared with the Deprecated List keyword will cause a deprecated warning to be raised when the function is referenced (but not when it's declared).

In this way API functions (or methods) can be declared as Deprecated List before eventual removal from the API set.