<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../style.css">
<title>
Gambas Documentation - Method Declaration
</title>
</head>
<table class="none" width="100%" cellpadding="0" cellspacing="0" border="0">
<tr><td align="left">
<font size="-1">
<a href="../../help+en"><img class="flag" alt="Home" border="0" src="../../img/lang/en.png" align="center"></a>&nbsp;
<a href="../lang+en">Up</a>&nbsp;
<a href="me+en">Previous</a>&nbsp;
<a href="mid+en">Next</a>&nbsp;
</td></tr></table>
<div class="notab">
<h1>
Method Declaration
</h1>
<h2>Procedures</h2>
<p>
<div class="black"><font size="-2"><b>Syntax</b></font></div>
<pre class="syntax">[ <b>STATIC</b> ] { <b>PUBLIC</b> | <b>PRIVATE</b> } { <b>PROCEDURE</b> | <b>SUB</b> }
&nbsp;&nbsp;<u>Identifier</u>
&nbsp;&nbsp;<b>(</b>
&nbsp;&nbsp;&nbsp;&nbsp;[ <u>Parameter</u> <b>AS</b> <u>Datatype</u> [ <b>,</b> ... ] ] [ <b>,</b> ]
&nbsp;&nbsp;&nbsp;&nbsp;[ <b>OPTIONAL</b> <u>Optional Parameter</u> <b>AS</b> <u>Datatype</u> [ <b>,</b> ... ] ] [ <b>,</b> ] [ <b>...</b> ]
&nbsp;&nbsp;<b>)</b>
&nbsp;&nbsp;...
<b>END</b></pre><p>

This declares a procedure, i.e. a method that returns nothing.
<p>
The <a href="end+en">END</a> keyword indicates the end of the procedure.
<p>
<h2>Functions</h2>
<p>
<div class="black"><font size="-2"><b>Syntax</b></font></div>
<pre class="syntax">[ <b>STATIC</b> ] { <b>PUBLIC</b> | <b>PRIVATE</b> } { <b>FUNCTION</b> | <b>PROCEDURE</b> | <b>SUB</b> }
&nbsp;&nbsp;<u>Identifier</u>
&nbsp;&nbsp;<b>(</b>
&nbsp;&nbsp;&nbsp;&nbsp;[ <u>Parameter</u> <b>AS</b> <u>Datatype</u> [ <b>,</b> ... ] ] [ <b>,</b> ]
&nbsp;&nbsp;&nbsp;&nbsp;[ <b>OPTIONAL</b> <u>Optional Parameter</u> <b>AS</b> <u>Datatype</u> [ <b>,</b> ... ] ] [ <b>,</b> ] [ <b>...</b> ]
&nbsp;&nbsp;<b>)</b>
&nbsp;&nbsp;<b>AS</b> <u>Datatype</u>
&nbsp;&nbsp;...
<b>END</b></pre><p>

This declares a function, i.e. a method that returns a value.
<p>
The <a href="end+en">END</a> keyword indicates the end of the function.
<p>
The datatype of the return value must be specified.
<p>
<div class="warning"><table class="none" border="0"><tr><td width="40" valign="top"><img border="0" src="../../img/info.png" align="center"></td><td valign="top">
Of course, these declarations must be written on a unique line. They are separated there so that it is readable.
</td></tr></table></div>
<p>
The <a href="return+en">RETURN</a> value is passed back to the caller as argument of the <a href="return+en">RETURN</a> statement.
<p>
<div class="gray"><font size="-2"><b>Example</b></font></div>
<pre class="example">FUNCTION Calc(fX AS Float) AS Float
  RETURN Sin(fX) * Exp(- fX)
END

PUBLIC SUB Button1_Click()
  PRINT Calc(0), Calc(0.5), Calc(1)
END
<hr>0	0.290786288213	0.309559875653</pre>
<p>
<h2>Method Access</h2>
<p>
The method is accessible everywhere in the class it is declared.
<p>
<ul>
<li>If the <tt><b><a href="public+en">PUBLIC</a></b></tt> keyword is specified, it is also accessible to the other classes having a reference to an <a href="../def/object+en">object</a> of this class.
<p>
<li>If the <tt><b><a href="static+en">STATIC</a></b></tt> keyword is specified, the method can only access to the static variables of the class.
<p>
</ul>

<h2>Method Parameters</h2>
<p>
All method parameters are separated by commas.
<p>
<ul>
<li>If the <tt><b><a href="optional+en">OPTIONAL</a></b></tt> keyword is specified, all parameters after the keywords are optional. You can specify a default value after the parameter declaration by using the equal sign.
<p>
<li>If the parameters list end with <tt><b>...</b></tt>, then the method can take extra arguments. Every additional argument passed to the method is accessible with the <a href="../comp/gb/param+en">Param</a> class.
<p>
</ul>

<div class="gray"><font size="-2"><b>Example</b></font></div>
<pre class="example">STATIC PUBLIC PROCEDURE Main()
...
PUBLIC FUNCTION Calc(fA AS Float, fB AS Float) AS Float
...
PRIVATE SUB DoIt(sCommand AS String, OPTIONAL bSaveIt AS Boolean = TRUE)
...
STATIC PRIVATE FUNCTION MyPrintf(sFormat AS String, ...) AS Integer</pre>
<p>
<hr><b>See also</b><br>
<a href="vardecl+en">Variable Declaration</a>&nbsp;

</div>
</body>
</html>

