Qore Programming Language Reference Manual
1.12.1
|
A function is declared in Qore by using the keyword sub
(for subroutine) as follows.
[public] [synchronized] [deprecated]
[return_type] sub
function_name(
[param_type] $
var_name ...]) {
returns
keyword:[public] [synchronized] [deprecated]
sub
function_name(
[param_type] $
var_name ...]) returns
return_type {
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.
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.
$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.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).
Functions use the return statement to provide a return value to the caller.
Here is an example function declaration returning a value:
Functions may also be recursive. Here is an example of a recursive Qore function implementing the Fibonacci function:
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.
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.