\hypertarget{overloading_overloading_intro}{}\section{Overloading Introduction}\label{overloading_overloading_intro}
Functions and methods can be overloaded if parameter types are declared as in the following example\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{int sub example(int i) \{}
\DoxyCodeLine{    \mbox{\hyperlink{group__string__functions_ga32070517a9f8a70a32ef48f83a6f128e}{printf}}(\textcolor{stringliteral}{"i=\%d\(\backslash\)n"}, i);}
\DoxyCodeLine{    \textcolor{keywordflow}{return} i + 1;}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{string sub example(string str) \{}
\DoxyCodeLine{    \mbox{\hyperlink{group__string__functions_ga32070517a9f8a70a32ef48f83a6f128e}{printf}}(\textcolor{stringliteral}{"str=\%s\(\backslash\)n"}, str);}
\DoxyCodeLine{    \textcolor{keywordflow}{return} str + \textcolor{stringliteral}{"foo"};}
\DoxyCodeLine{\}}
\end{DoxyCode}


In this case, the first version ({\ttfamily example(int)}) will be executed if called with an integer argument, and the second ({\ttfamily example(string)}) if called with a string argument.

Qore looks at the arguments passed to the function or method and tries to find the best match. A perfect match is when all arguments exactly match the expected types. An imperfect match is when the declared type accepts the argument value, but it is not an exact match. Examples of imperfect matches are \mbox{\hyperlink{data_type_declarations_float_type}{float}} accepting an \mbox{\hyperlink{data_type_declarations_int_type}{int}} value, or a \mbox{\hyperlink{data_type_declarations_number_type}{number}} type accepting a \mbox{\hyperlink{data_type_declarations_float_type}{float}}.

When types are declared in the source, Qore uses this information to perform overloaded function and method resolution at parse time, which speeds up runtime execution. If there is not enough information to match the function or method variant at parse time, then Qore has to perform variant resolution at runtime, therefore it\textquotesingle{}s advisable to declare types whenever possible.

\begin{DoxySeeAlso}{See also}
\mbox{\hyperlink{parse_directives_require-types}{\%require-\/types}}
\end{DoxySeeAlso}
\hypertarget{overloading_overloaded_methods}{}\section{Overloaded Class Methods}\label{overloading_overloaded_methods}
Class methods may also be overloaded, but note that {\ttfamily destructor()}, {\ttfamily copy()}, \mbox{\hyperlink{qore_classes_methodGate_methods}{method\+Gate()}}, \mbox{\hyperlink{qore_classes_memberGate_methods}{member\+Gate()}}, and \mbox{\hyperlink{qore_classes_memberNotification_methods}{member\+Notification()}} methods may not be overloaded, but all other methods may be overloaded.

When resolving an overloaded method in a class hierarchy, the first method that matches the arguments is selected, so even if a better match might be declared in a parent class, if any method variant matches in a class, then that class\textquotesingle{}s method is used. The same rules are used with parse-\/time resolution as at runtime.

For calls and call references with a class prefix, when \mbox{\hyperlink{parse_directives_allow-bare-refs}{\%allow-\/bare-\/refs}} or \mbox{\hyperlink{parse_directives_new-style}{\%new-\/style}} are in effect, static methods are searched first, then, if no match is found in the hierarchy, normal methods are searched. For calls without a class prefix, normal methods are searched first, then static methods.

\begin{DoxySeeAlso}{See also}
\mbox{\hyperlink{qore_classes}{Classes}} for more information 
\end{DoxySeeAlso}
