Difference between revisions of "NL/Documentation/BASIC Guide/Procedures and Functions"

From Apache OpenOffice Wiki
Jump to: navigation, search
(Functies)
(Terminating Procedures and Functions Prematurely)
Line 68: Line 68:
 
Als het type retourwaarde niet is gespecificeerd (zie het eerste voorbeeld van deze pagina), geeft de functie een variant terug.
 
Als het type retourwaarde niet is gespecificeerd (zie het eerste voorbeeld van deze pagina), geeft de functie een variant terug.
  
== Terminating Procedures and Functions Prematurely ==
+
== Voortijdig beëindigen van procedures en functies ==
  
In {{OOo}} Basic, you can use the <tt>Exit Sub</tt> and <tt>Exit Function</tt> 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.
+
In {{OOo}} BASIC, kunt u de opdrachten <tt>Exit Sub</tt> en <tt>Exit Function</tt> gebruiken om een procedure of functie voortijdig te beëindigen, bijvoorbeeld voor foutafhandeling. Deze opdrachten stoppen de procedure of functie en laten het programma terugkeren naar het punt waarop de procedure en/of functie werd aangeroepen.
  
The following example shows a procedure which terminates implementation when the <tt>ErrorOccured</tt> variable has the value <tt>True</tt>.
+
Het volgende voorbeeld toont een procedure die verder uitvoering beëindigt als de  variabele <tt>ErrorOccured</tt> de waarde <tt>True</tt> heeft.
  
 
<source lang="oobas">
 
<source lang="oobas">

Revision as of 17:42, 21 January 2013

Book.png


Procedures en functies vormen belangrijke punten in de structuur van een programma. Zij verschaffen het raamwerk om een complex probleem op te delen in verschillende sub-taken.

Procedures

Een procedure voert een actie uit zonder een expliciete waarde te verschaffen. Zijn syntaxis is

Sub Test
  ' ... hier staat de actuele code van de procedure
End Sub

Het voorbeeld definieert een procedure genaamd Test die code bevat die kan worden benaderd vanaf elk punt in het programma. De aanroep wordt gemaakt door de procedurenaam op de relevante plaats in het programma in te voeren:

Functies

Een function, net als een procedure, combineert een blok programma's om te worden uitgevoerd in één logische eenheid. Echter, anders dan een procedure, verschaft een functie een retourwaarde.

Function Test
  ' ... hier staat de actuele code van de functie
  Test = 123
End Function

De retourwaarde wordt toegewezen via een eenvoudige toewijzing. De toewijzing hoeft niet te worden geplaatst aan het einde van de functie, maar kan op elke plaats in de functie worden gemaakt.

De voorgaande functie kan als volgt worden aangeroepen binnen het programma:

Dim A
A = Test

De code definieert een variabele A en wijst daar het resultaat van de functie Test aan toe.

De retourwaarde kan binnen de functie meerdere malen worden overschreven. Zoals met klassieke toewijzing van variabelen, geeft de functie in dit voorbeeld de waarde weer die daaraan het laatste was toegekend.

Function Test
  Test = 12
  ' ... 
  Test = 123
End Function

In dit voorbeeld is de retourwaarde van de functie 123.


Als een toewijzing is gestopt, geeft de functie een waarde nul weer (getal 0 voor numerieke waarden en een blanco voor tekenreeksen).

De retourwaarde van een functie kan elk type zijn. Het type wordt gedeclareerd op dezelfde manier als een declaratie van een variabele:

Function Test As Integer
  ' ... hier staat de actuele code van de functie
End Function

Als het type retourwaarde niet is gespecificeerd (zie het eerste voorbeeld van deze pagina), geeft de functie een variant terug.

Voortijdig beëindigen van procedures en functies

In Apache OpenOffice BASIC, kunt u de opdrachten Exit Sub en Exit Function gebruiken om een procedure of functie voortijdig te beëindigen, bijvoorbeeld voor foutafhandeling. Deze opdrachten stoppen de procedure of functie en laten het programma terugkeren naar het punt waarop de procedure en/of functie werd aangeroepen.

Het volgende voorbeeld toont een procedure die verder uitvoering beëindigt als de variabele ErrorOccured de waarde True heeft.

Sub Test
  Dim ErrorOccured As Boolean
  ' ...
  If ErrorOccured Then
    Exit Sub
  End If
  ' ...
End Sub

Passing Parameters

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 A and a string B as parameters.

Sub Test (A As Integer, B As String)
  ' ...
End Sub

Parameters are normally passed by Reference in Apache OpenOffice Basic. Changes made to the variables are retained when the procedure or function is exited:

Sub Test 
  Dim A As Integer
  A = 10
  ChangeValue(A)
  ' The parameter A now has the value 20
End Sub
 
Sub ChangeValue(TheValue As Integer)
  TheValue = 20
End Sub

In this example, the value A that is defined in the Test function is passed as a parameter to the ChangeValue function. The value is then changed to 20 and passed to TheValue, which is retained when the function is exited.

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 ByVal keyword precedes the variable declaration in the function header.

In the preceding example, if we replace the ChangeValue function then the superordinate variable A remains unaffected by this change. After the call for the ChangeValue function, variable A retains the value 10.

Sub ChangeValue(ByVal TheValue As Integer)
  TheValue = 20
End Sub
Documentation note.png VBA : The method for passing parameters to procedures and functions in Apache OpenOffice Basic is virtually identical to that in VBA. By default, the parameters are passed by reference. To pass parameters as values, use the ByVal keyword. In VBA, you can also use the keyword ByRef to force a parameter to be passed by reference. Apache OpenOffice Basic recognizes but ignores this keyword, because this is already the default procedure in Apache OpenOffice Basic.


Optional Parameters

Functions and procedures can only be called up if all the necessary parameters are passed during the call.

Apache OpenOffice Basic lets you define parameters as optional, that is, if the corresponding values are not included in a call, Apache OpenOffice Basic passes an empty parameter. In the following example the A parameter is obligatory, whereas the B parameter is optional.

Sub Test(A As Integer, Optional B As Integer)
  ' ...
End Sub

The IsMissing function checks whether a parameter has been passed or is left out.

Sub Test(A As Integer, Optional B As Integer)
  Dim B_Local As Integer
  ' Check whether B parameter is actually present         
  If Not IsMissing (B) Then   
    B_Local = B      ' B parameter present
  Else
    B_Local = 0      ' B parameter missing -> default value 0
  End If
  ' ... Start the actual function
End Sub

The example first tests whether the B parameter has been passed and, if necessary, passes the same parameter to the internal B_Local variable. If the corresponding parameter is not present, then a default value (in this instance, the value 0) is passed to B_Local rather than the passed parameter.

Documentation note.png VBA : The ParamArray keyword present in VBA is not supported in Apache OpenOffice Basic.


Recursion

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.

The following example uses a recursive function to calculate the factorial of the numbers 42, -42, and 3.14:

Sub Main
  Msgbox CalculateFactorial(  42 )    ' Displays 1,40500611775288E+51
  Msgbox CalculateFactorial( -42 )    ' Displays "Invalid number for factorial!"
  Msgbox CalculateFactorial( 3.14 )   ' Displays "Invalid number for factorial!"
End Sub 
 
Function CalculateFactorial( Number )
  If Number < 0 Or Number <> Int( Number ) Then
    CalculateFactorial = "Invalid number for factorial!"
  ElseIf Number = 0 Then
    CalculateFactorial = 1
  Else
    ' This is the recursive call:
    CalculateFactorial = Number * CalculateFactorial( Number - 1 )
  Endif
End Function

The example returns the factorial of the number 42 by recursively calling the CalculateFactorial function until it reaches the base condition of 0! = 1.

Template:Documentation/Note


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools