\hypertarget{qore_functions_function_declarations}{}\section{Function Declarations}\label{qore_functions_function_declarations}
A function is declared in Qore by using the keyword {\ttfamily {\bfseries sub}} (for subroutine) as follows.

\begin{DoxyParagraph}{Function Declaration Syntax}
{\ttfamily \mbox{[}\hyperlink{qore_modules_mod_public}{public}\mbox{]} \mbox{[}\hyperlink{threading_synchronized}{synchronized}\mbox{]} \mbox{[}\hyperlink{qore_functions_deprecated}{deprecated}\mbox{]}} {\itshape \hyperlink{data_type_declarations}{\mbox{[}return\+\_\+type\mbox{]}}} {\ttfamily {\bfseries sub}} {\itshape function\+\_\+name}{\ttfamily (}{\itshape \hyperlink{data_type_declarations}{\mbox{[}param\+\_\+type\mbox{]}}} {\ttfamily \$}{\itshape var\+\_\+name} ...{\ttfamily \mbox{]}) \{}~\newline
 ~~~~{\itshape \hyperlink{statements}{statement(s)...}}~\newline
 \}~\newline
~\newline
 or the deprecated alternate syntax with the {\ttfamily {\bfseries returns}} keyword\+:~\newline
 {\ttfamily \mbox{[}\hyperlink{qore_modules_mod_public}{public}\mbox{]} \mbox{[}\hyperlink{threading_synchronized}{synchronized}\mbox{]} \mbox{[}\hyperlink{qore_functions_deprecated}{deprecated}\mbox{]}} {\ttfamily {\bfseries sub}} {\itshape function\+\_\+name}{\ttfamily (}{\itshape \hyperlink{data_type_declarations}{\mbox{[}param\+\_\+type\mbox{]}}} {\ttfamily \$}{\itshape var\+\_\+name} ...{\ttfamily \mbox{]}) {\bfseries returns}} {\itshape \hyperlink{data_type_declarations}{return\+\_\+type}} {\ttfamily \{}~\newline
 ~~~~{\itshape \hyperlink{statements}{statement(s)...}}~\newline
 \}~\newline
 Function names must be valid Qore identifiers.
\end{DoxyParagraph}
When defining a \hyperlink{qore_modules_user_modules}{user module}, the function definition can be preceded by \hyperlink{qore_modules_mod_public}{public}, which means that the function variant will be available (imported) in the \hyperlink{class_qore_1_1_program}{Program} object importing the module. See \hyperlink{qore_modules_mod_public}{public} for more information.\hypertarget{qore_functions_function_parameters}{}\subsection{Function Parameters}\label{qore_functions_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 \hyperlink{overloading}{overloading} the function.

Variables passed as function arguments are passed by value by default, unless the caller places a {\ttfamily \char`\"{}\textbackslash{}\char`\"{}} 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 \hyperlink{operators_background}{background} expression.

\begin{DoxyNote}{Note}
In order to process a variable number of arguments to a function, use \hyperlink{expressions_implicit_arguments}{implicit argument references} ({\ttfamily \$1}) or the {\ttfamily \$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 {\ttfamily \$argv} variable.
\end{DoxyNote}
\hypertarget{qore_functions_function_return_types}{}\subsection{Function Return Type Declarations}\label{qore_functions_function_return_types}
The return type of the function can be given by placing a type declaration before the {\ttfamily {\bfseries sub}} keyword (the older syntax with the {\ttfamily {\bfseries returns}} keyword after the parameter list is still accepted as well).

\begin{DoxyNote}{Note}
Parameter and return types are required when the \hyperlink{group__parse__options_ga66847391bb96dac88c3fee210720bbb3}{Qore\+::\+P\+O\+\_\+\+R\+E\+Q\+U\+I\+R\+E\+\_\+\+T\+Y\+P\+E\+S} or \hyperlink{group__parse__options_ga196e048def21cb147b21a4871c4e9b29}{Qore\+::\+P\+O\+\_\+\+R\+E\+Q\+U\+I\+R\+E\+\_\+\+P\+R\+O\+T\+O\+T\+Y\+P\+E\+S} parse options are set.
\end{DoxyNote}
Functions use the \hyperlink{statements_return}{return statement} to provide a return value to the caller.\hypertarget{qore_functions_function_examples}{}\subsection{Simple Example Functions}\label{qore_functions_function_examples}
Here is an example function declaration returning a value\+: 
\begin{DoxyCode}
\textcolor{preprocessor}{#!/usr/bin/qore}
\textcolor{preprocessor}{# function declaration example}

\textcolor{keywordtype}{int} sub print\_string(\textcolor{keywordtype}{string} $string) \{
    \hyperlink{group__string__functions_ga996c5d1686fbc245f0bc7a02012e6a33}{print}(\textcolor{stringliteral}{"%s\(\backslash\)n"}, $string);
    \textcolor{keywordflow}{return} 1;
\}
\end{DoxyCode}


Functions may also be recursive. Here is an example of a recursive Qore function implementing the Fibonacci function\+: 
\begin{DoxyCode}
\textcolor{preprocessor}{#!/usr/bin/qore}
\textcolor{preprocessor}{#}
\textcolor{preprocessor}{# recursive function example}

\textcolor{keywordtype}{int} sub fibonacci(\textcolor{keywordtype}{int} $num) \{
    \textcolor{keywordflow}{if} ($num == 1)
        \textcolor{keywordflow}{return} 1;
    \textcolor{keywordflow}{return} $num * fibonacci($num - 1);
\}
\end{DoxyCode}


\begin{DoxyNote}{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.
\end{DoxyNote}
\hypertarget{qore_functions_synchronized_functions}{}\subsection{\char`\"{}\+Synchronized\char`\"{} Functions}\label{qore_functions_synchronized_functions}
Functions declared with the {\ttfamily {\bfseries 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 {\ttfamily T\+H\+R\+E\+A\+D-\/\+D\+E\+A\+D\+L\+O\+C\+K} exception is thrown.

\begin{DoxySeeAlso}{See also}
\hyperlink{threading_synchronized}{synchronized}
\end{DoxySeeAlso}
\hypertarget{qore_functions_deprecated}{}\subsection{\char`\"{}\+Deprecated\char`\"{} Functions}\label{qore_functions_deprecated}
Functions declared with the {\ttfamily {\bfseries deprecated}} keyword will cause a \hyperlink{warnings_deprecated-warning}{deprecated warning} to be raised when the function is referenced (but not when it's declared).

In this way A\+P\+I functions (or methods) can be declared as {\ttfamily {\bfseries deprecated}} before eventual removal from the A\+P\+I set. 
