\hypertarget{qore_functions_function_declarations}{}\doxysection{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{[}\mbox{\hyperlink{qore_modules_mod_public}{public}}\mbox{]} \mbox{[}\mbox{\hyperlink{threading_synchronized}{synchronized}}\mbox{]} \mbox{[}\mbox{\hyperlink{deprecated}{deprecated}}\mbox{]}} {\itshape \mbox{\hyperlink{data_type_declarations}{\mbox{[}return\+\_\+type\mbox{]}}}} {\ttfamily {\bfseries{sub}}} {\itshape function\+\_\+name}{\ttfamily (}{\itshape \mbox{\hyperlink{data_type_declarations}{\mbox{[}param\+\_\+type\mbox{]}}}} {\ttfamily \$}{\itshape var\+\_\+name} ...{\ttfamily \mbox{]}) \{}~\newline ~~~~{\itshape \mbox{\hyperlink{statements}{statement(s)...}}}~\newline \}~\newline ~\newline or the deprecated alternate syntax with the {\ttfamily {\bfseries{returns}}} keyword\+:~\newline {\ttfamily \mbox{[}\mbox{\hyperlink{qore_modules_mod_public}{public}}\mbox{]} \mbox{[}\mbox{\hyperlink{threading_synchronized}{synchronized}}\mbox{]} \mbox{[}\mbox{\hyperlink{deprecated}{deprecated}}\mbox{]}} {\ttfamily {\bfseries{sub}}} {\itshape function\+\_\+name}{\ttfamily (}{\itshape \mbox{\hyperlink{data_type_declarations}{\mbox{[}param\+\_\+type\mbox{]}}}} {\ttfamily \$}{\itshape var\+\_\+name} ...{\ttfamily \mbox{]}) {\bfseries{returns}}} {\itshape \mbox{\hyperlink{data_type_declarations}{return\+\_\+type}}} {\ttfamily \{}~\newline ~~~~{\itshape \mbox{\hyperlink{statements}{statement(s)...}}}~\newline \}~\newline Function names must be valid Qore identifiers. \end{DoxyParagraph} When defining a \mbox{\hyperlink{qore_modules_user_modules}{user module}}, the function definition can be preceded by \mbox{\hyperlink{qore_modules_mod_public}{public}}, which means that the function variant will be available (imported) in the \mbox{\hyperlink{class_qore_1_1_program}{Program}} object importing the module. See \mbox{\hyperlink{qore_modules_mod_public}{public}} for more information.\hypertarget{qore_functions_function_parameters}{}\doxysubsection{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 \mbox{\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 \mbox{\hyperlink{operators_background}{background}} expression. \begin{DoxyNote}{Note} In order to process a variable number of arguments to a function, use \mbox{\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}{}\doxysubsection{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 \mbox{\hyperlink{group__parse__options_ga66847391bb96dac88c3fee210720bbb3}{Qore\+::\+PO\+\_\+\+REQUIRE\+\_\+\+TYPES}} or \mbox{\hyperlink{group__parse__options_ga196e048def21cb147b21a4871c4e9b29}{Qore\+::\+PO\+\_\+\+REQUIRE\+\_\+\+PROTOTYPES}} parse options are set. If the return type is missing, it is assumed to be of type \mbox{\hyperlink{data_type_declarations_any_type}{any}}. In case \mbox{\hyperlink{group__parse__options_ga66847391bb96dac88c3fee210720bbb3}{Qore\+::\+PO\+\_\+\+REQUIRE\+\_\+\+TYPES}} or \mbox{\hyperlink{group__parse__options_ga196e048def21cb147b21a4871c4e9b29}{Qore\+::\+PO\+\_\+\+REQUIRE\+\_\+\+PROTOTYPES}} parse options are set, and the return type is missing from function\textquotesingle{}s declaration, it is assumed to be of type \mbox{\hyperlink{basic_data_types_nothing}{NOTHING}}. \end{DoxyNote} Functions use the \mbox{\hyperlink{statements_return}{return statement}} to provide a return value to the caller.\hypertarget{qore_functions_function_examples}{}\doxysubsection{Simple Example Functions}\label{qore_functions_function_examples} Here is an example function declaration returning a value\+: \begin{DoxyCode}{0} \DoxyCodeLine{\textcolor{comment}{\#!/usr/bin/qore}} \DoxyCodeLine{\textcolor{comment}{\# function declaration example}} \DoxyCodeLine{\%new-\/style} \DoxyCodeLine{} \DoxyCodeLine{int sub print\_string(string string) \{} \DoxyCodeLine{ \mbox{\hyperlink{group__string__functions_ga996c5d1686fbc245f0bc7a02012e6a33}{print}}(\textcolor{stringliteral}{"{}\%s\(\backslash\)n"{}}, string);} \DoxyCodeLine{ \textcolor{keywordflow}{return} 1;} \DoxyCodeLine{\}} \end{DoxyCode} Functions may also be recursive. Here is an example of a recursive Qore function implementing the Fibonacci function\+: \begin{DoxyCode}{0} \DoxyCodeLine{\textcolor{comment}{\#!/usr/bin/qore}} \DoxyCodeLine{\textcolor{comment}{\#}} \DoxyCodeLine{\textcolor{comment}{\# recursive function example}} \DoxyCodeLine{\%new-\/style} \DoxyCodeLine{} \DoxyCodeLine{int sub fibonacci(int num) \{} \DoxyCodeLine{ \textcolor{keywordflow}{if} (num == 1)} \DoxyCodeLine{ \textcolor{keywordflow}{return} 1;} \DoxyCodeLine{ \textcolor{keywordflow}{return} num * fibonacci(num -\/ 1);} \DoxyCodeLine{\}} \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}{}\doxysubsection{\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\textquotesingle{}s deadlock detection framework, therefore if a deadlock is detected, a {\ttfamily THREAD-\/\+DEADLOCK} exception is thrown. \begin{DoxySeeAlso}{See also} \mbox{\hyperlink{threading_synchronized}{synchronized}} \end{DoxySeeAlso} \hypertarget{qore_functions_deprecated-functions}{}\doxysubsection{\char`\"{}\+Deprecated\char`\"{} Functions}\label{qore_functions_deprecated-functions} Functions declared with the \mbox{\hyperlink{deprecated}{Deprecated List}} keyword will cause a \mbox{\hyperlink{warnings_deprecated-warning}{deprecated warning}} to be raised when the function is referenced (but not when it\textquotesingle{}s declared). In this way API functions (or methods) can be declared as \mbox{\hyperlink{deprecated}{Deprecated List}} before eventual removal from the API set.