<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.harlamenkov.ru/index.php?action=history&amp;feed=atom&amp;title=EN%2Fdocumentation%2Fbasic_guide%2Fprocedures_and_functions</id>
	<title>EN/documentation/basic guide/procedures and functions - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.harlamenkov.ru/index.php?action=history&amp;feed=atom&amp;title=EN%2Fdocumentation%2Fbasic_guide%2Fprocedures_and_functions"/>
	<link rel="alternate" type="text/html" href="https://wiki.harlamenkov.ru/index.php?title=EN/documentation/basic_guide/procedures_and_functions&amp;action=history"/>
	<updated>2026-11-11T14:06:07Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.33.0</generator>
	<entry>
		<id>https://wiki.harlamenkov.ru/index.php?title=EN/documentation/basic_guide/procedures_and_functions&amp;diff=15889&amp;oldid=prev</id>
		<title>Sancho: Created page with &quot;&lt;section begin=title /&gt;&lt;noinclude&gt;{{DISPLAYTITLE:&lt;/noinclude&gt;Procedures and Functions&lt;noinclude&gt;}}&lt;/noinclude&gt;&lt;section end=title /&gt;{{BreadCrumbL10n}}   {{Documentation/BASICGuide...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.harlamenkov.ru/index.php?title=EN/documentation/basic_guide/procedures_and_functions&amp;diff=15889&amp;oldid=prev"/>
		<updated>2011-09-01T15:32:34Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;&amp;lt;section begin=title /&amp;gt;&amp;lt;noinclude&amp;gt;{{DISPLAYTITLE:&amp;lt;/noinclude&amp;gt;Procedures and Functions&amp;lt;noinclude&amp;gt;}}&amp;lt;/noinclude&amp;gt;&amp;lt;section end=title /&amp;gt;{{BreadCrumbL10n}}   {{Documentation/BASICGuide...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;section begin=title /&amp;gt;&amp;lt;noinclude&amp;gt;{{DISPLAYTITLE:&amp;lt;/noinclude&amp;gt;Procedures and Functions&amp;lt;noinclude&amp;gt;}}&amp;lt;/noinclude&amp;gt;&amp;lt;section end=title /&amp;gt;{{BreadCrumbL10n}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Documentation/BASICGuideTOC&lt;br /&gt;
|ShowPrevNext=block&lt;br /&gt;
|ShowPrevPage=block&lt;br /&gt;
|PrevPage=EN/documentation/basic_guide/loops&lt;br /&gt;
|NextPage=EN/documentation/basic_guide/error_handling&lt;br /&gt;
|lang=block&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
__NOTOC__&lt;br /&gt;
Procedures and functions form pivotal points in the structure of a program. They provide the framework for dividing a complex problem into various sub-tasks.&lt;br /&gt;
&lt;br /&gt;
== Procedures ==&lt;br /&gt;
&lt;br /&gt;
A '''procedure''' executes an action without providing an explicit value. Its syntax is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Sub Test&lt;br /&gt;
  ' ... here is the actual code of the procedure&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example defines a procedure called &amp;lt;tt&amp;gt;Test&amp;lt;/tt&amp;gt; that contains code that can be accessed from any point in the program. The call is made by entering the procedure name at the relevant point of the program.&lt;br /&gt;
&lt;br /&gt;
== Functions ==&lt;br /&gt;
&lt;br /&gt;
A '''function''', just like a procedure, combines a block of programs to be executed into one logical unit. However, unlike a procedure, a function provides a return value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Function Test&lt;br /&gt;
  ' ... here is the actual code of the function&lt;br /&gt;
  Test = 123&lt;br /&gt;
End Function &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The return value is assigned using simple assignment. The assignment does not need to be placed at the end of the function, but can be made anywhere in the function.&lt;br /&gt;
&lt;br /&gt;
The preceding function can be called within a program as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Dim A&lt;br /&gt;
A = Test&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code defines a variable &amp;lt;tt&amp;gt;A&amp;lt;/tt&amp;gt; and assigns the result of the &amp;lt;tt&amp;gt;Test&amp;lt;/tt&amp;gt; function to it.&lt;br /&gt;
&lt;br /&gt;
The return value can be overwritten several times within the function. As with classic variable assignment, the function in this example returns the value that was last assigned to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Function Test&lt;br /&gt;
  Test = 12&lt;br /&gt;
  ' ... &lt;br /&gt;
  Test = 123&lt;br /&gt;
End Function &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, the return value of the function is 123.&lt;br /&gt;
&lt;br /&gt;
If nothing is assigned, the function returns a &amp;lt;tt&amp;gt;zero&amp;lt;/tt&amp;gt; value (number 0 for numerical values and a blank for strings).&lt;br /&gt;
&lt;br /&gt;
The return value of a function can be any type. The type is declared in the same way as a variable declaration:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Function Test As Integer&lt;br /&gt;
  ' ... here is the actual code of the function&lt;br /&gt;
End Function &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
If the return type is not specified (see first example of this page), the function returns a variant.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Terminating Procedures and Functions Prematurely ==&lt;br /&gt;
&lt;br /&gt;
In {{OOo}} Basic, you can use the &amp;lt;tt&amp;gt;Exit Sub&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;Exit Function&amp;lt;/tt&amp;gt; commands to terminate a procedure or function prematurely, for example, for error handling. These commands stop the procedure or function and return the program to the point at which the procedure or function was called up.&lt;br /&gt;
&lt;br /&gt;
The following example shows a procedure which terminates implementation when the &amp;lt;tt&amp;gt;ErrorOccured&amp;lt;/tt&amp;gt; variable has the value &amp;lt;tt&amp;gt;True&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Sub Test&lt;br /&gt;
  Dim ErrorOccured As Boolean&lt;br /&gt;
  ' ...&lt;br /&gt;
  If ErrorOccured Then&lt;br /&gt;
    Exit Sub&lt;br /&gt;
  End If&lt;br /&gt;
  ' ...&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Passing Parameters ==&lt;br /&gt;
&lt;br /&gt;
Functions and procedures can receive one or more parameters. Essential parameters must be enclosed in parentheses after the function or procedure names. The following example defines a procedure that expects an integer value &amp;lt;tt&amp;gt;A&amp;lt;/tt&amp;gt; and a string &amp;lt;tt&amp;gt;B&amp;lt;/tt&amp;gt; as parameters.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Sub Test (A As Integer, B As String)&lt;br /&gt;
  ' ...&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Parameters are normally [http://en.wikipedia.org/wiki/Pass_by_reference#Call_by_reference passed by '''Reference'''] in {{OOo}} Basic. Changes made to the variables are retained when the procedure or function is exited:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Sub Test &lt;br /&gt;
  Dim A As Integer&lt;br /&gt;
  A = 10&lt;br /&gt;
  ChangeValue(A)&lt;br /&gt;
  ' The parameter A now has the value 20&lt;br /&gt;
End Sub&lt;br /&gt;
&lt;br /&gt;
Sub ChangeValue(TheValue As Integer)&lt;br /&gt;
  TheValue = 20&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, the value &amp;lt;tt&amp;gt;A&amp;lt;/tt&amp;gt; that is defined in the &amp;lt;tt&amp;gt;Test&amp;lt;/tt&amp;gt; function is passed as a parameter to the &amp;lt;tt&amp;gt;ChangeValue&amp;lt;/tt&amp;gt; function. The value is then changed to 20 and passed to &amp;lt;tt&amp;gt;TheValue&amp;lt;/tt&amp;gt;, which is retained when the function is exited.&lt;br /&gt;
&lt;br /&gt;
You can also pass a parameter as a '''value''' if you do not want subsequent changes to the parameter to affect the value that is originally passed. To specify that a parameter is to be passed as a value, ensure that the &amp;lt;tt&amp;gt;ByVal&amp;lt;/tt&amp;gt; keyword precedes the variable declaration in the function header.&lt;br /&gt;
&lt;br /&gt;
In the preceding example, if we replace the &amp;lt;tt&amp;gt;ChangeValue&amp;lt;/tt&amp;gt; function then the superordinate variable A remains unaffected by this change. After the call for the &amp;lt;tt&amp;gt;ChangeValue&amp;lt;/tt&amp;gt; function, variable &amp;lt;tt&amp;gt;A&amp;lt;/tt&amp;gt; retains the value 10.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Sub ChangeValue(ByVal TheValue As Integer)&lt;br /&gt;
  TheValue = 20&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{VBAnote|The method for passing parameters to procedures and functions in {{OOo}} Basic is virtually identical to that in VBA. By default, the parameters are passed by reference. To pass parameters as values, use the &amp;lt;tt&amp;gt;ByVal&amp;lt;/tt&amp;gt; keyword. In VBA, you can also use the keyword &amp;lt;tt&amp;gt;ByRef&amp;lt;/tt&amp;gt; to force a parameter to be passed by reference. {{OOo}} Basic recognizes but ignores this keyword, because this is already the default procedure in {{OOo}} Basic.}}&lt;br /&gt;
&lt;br /&gt;
== Optional Parameters ==&lt;br /&gt;
&lt;br /&gt;
Functions and procedures can only be called up if all the necessary parameters are passed during the call. &lt;br /&gt;
&lt;br /&gt;
{{OOo}} Basic lets you define parameters as '''optional''', that is, if the corresponding values are not included in a call, {{OOo}} Basic passes an empty parameter. In the following example the &amp;lt;tt&amp;gt;A&amp;lt;/tt&amp;gt; parameter is obligatory, whereas the &amp;lt;tt&amp;gt;B&amp;lt;/tt&amp;gt; parameter is optional. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Sub Test(A As Integer, Optional B As Integer)&lt;br /&gt;
  ' ...&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;IsMissing&amp;lt;/tt&amp;gt; function checks whether a parameter has been passed or is left out. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Sub Test(A As Integer, Optional B As Integer)&lt;br /&gt;
  Dim B_Local As Integer&lt;br /&gt;
  ' Check whether B parameter is actually present         &lt;br /&gt;
  If Not IsMissing (B) Then   &lt;br /&gt;
    B_Local = B      ' B parameter present&lt;br /&gt;
  Else&lt;br /&gt;
    B_Local = 0      ' B parameter missing -&amp;gt; default value 0&lt;br /&gt;
  End If&lt;br /&gt;
  ' ... Start the actual function&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example first tests whether the &amp;lt;tt&amp;gt;B&amp;lt;/tt&amp;gt; parameter has been passed and, if necessary, passes the same parameter to the internal &amp;lt;tt&amp;gt;B_Local&amp;lt;/tt&amp;gt; variable. If the corresponding parameter is not present, then a default value (in this instance, the value 0) is passed to &amp;lt;tt&amp;gt;B_Local&amp;lt;/tt&amp;gt; rather than the passed parameter.&lt;br /&gt;
&lt;br /&gt;
{{VBAnote|The &amp;lt;tt&amp;gt;ParamArray&amp;lt;/tt&amp;gt; keyword present in VBA is not supported in {{OOo}} Basic.}}&lt;br /&gt;
&lt;br /&gt;
== Recursion ==&lt;br /&gt;
&lt;br /&gt;
A recursive procedure or function is one that has the ability to call itself until it detects that some base condition has been satisfied. When the function is called with the base condition, a result is returned. &lt;br /&gt;
&lt;br /&gt;
The following example uses a recursive function to calculate the factorial of the numbers &amp;lt;tt&amp;gt;42&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;-42&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;3.14&amp;lt;/tt&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Sub Main&lt;br /&gt;
  Msgbox CalculateFactorial(  42 )    ' Displays 1,40500611775288E+51&lt;br /&gt;
  Msgbox CalculateFactorial( -42 )    ' Displays &amp;quot;Invalid number for factorial!&amp;quot;&lt;br /&gt;
  Msgbox CalculateFactorial( 3.14 )   ' Displays &amp;quot;Invalid number for factorial!&amp;quot;&lt;br /&gt;
End Sub &lt;br /&gt;
&lt;br /&gt;
Function CalculateFactorial( Number )&lt;br /&gt;
  If Number &amp;lt; 0 Or Number &amp;lt;&amp;gt; Int( Number ) Then&lt;br /&gt;
    CalculateFactorial = &amp;quot;Invalid number for factorial!&amp;quot;&lt;br /&gt;
  ElseIf Number = 0 Then&lt;br /&gt;
    CalculateFactorial = 1&lt;br /&gt;
  Else&lt;br /&gt;
    ' This is the recursive call:&lt;br /&gt;
    CalculateFactorial = Number * CalculateFactorial( Number - 1 )&lt;br /&gt;
  Endif&lt;br /&gt;
End Function &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example returns the factorial of the number &amp;lt;tt&amp;gt;42&amp;lt;/tt&amp;gt; by recursively calling the &amp;lt;tt&amp;gt;CalculateFactorial&amp;lt;/tt&amp;gt; function until it reaches the base condition of &amp;lt;tt&amp;gt;0! = 1&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{{Note|The recursion levels are set at different levels based on the software platform. For Windows the recursion level is 5800. For Solaris and Linux, an evaluation of the stacksize is performed and the recursion level is calculated.}}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
{{PDL1}}&lt;/div&gt;</summary>
		<author><name>Sancho</name></author>
		
	</entry>
</feed>