The statement Return or Return[ expression] terminates a procedure or function as a control element.
The examples are intended to demonstrate the use of the control element Return or Return[ Expression] in different applications.
Example 1
The classic example of a function such as y = f(x) = x² - 3 - as a definite mapping - is certainly still familiar to you from your school days:
Public Function f(x AS Float) AS Float RETURN (x * x) - 3 End
with the call in another procedure:
Public Sub Calculate() Dim y as Float y = f(-5.66) … End
Example 2
The function IsIntegerRange(..) returns either True or False as a function value, depending on the argument iPSum:
Private Function IsIntegerRange(iPSumme As Integer) As Boolean If iPSumme > 2147483647 OR If iPSumme < 0 Then btnReset.SetFocus Return False Else Return True Endif End ' IsIntegerRange(...)
Example 3
The procedure CheckInput(..) can be used whenever only characters from a given input alphabet are accepted for an input component:
Public Sub txbInputPolynom1_KeyPress() CheckInput("+-,.^0123456789x") End Public Sub CheckInput(sAllowed As String) Select Case Key.Code Case Key.Left, Key.Right, Key.BackSpace, Key.Delete, Key.End, Key.Home, Key.Enter, Key.Return Return Default If Key.Text And If InStr(sAllowed, Key.Text) Then Return Endif End Select Stop Event End
With selected keys and characters from the input alphabet, the procedure CheckInput (..) is aborted! Via' Stop Event' all invalid characters and keystrokes are ignored.
Example 4
In the following section of the source code, the procedure is terminated if an error occurred when creating the directory with the specified path because the existence of the folder is obviously a mandatory prerequisite for program execution:
Public Sub MyProzedur() … If Not Exist(sSVNPfad) Then Try Mkdir sSVNPfad If Error = True Then Message.Error("Error creating the folder" & sSVNPfad) RETURN Endif ' Error = True? Endif ' Not Exist(SVNPfad? …
Example 5
As a function value, an output list is returned as a string array after removing all elements that occur more than once in the input list (string array) passed as an argument:
Public Function RemoveMultiple(aInputList As String[]) As String[] Dim iCount As Integer Dim iIndex As Integer Dim sElement As String Dim aOutputList As String[] aOutputList = aInputList iIndex = 0 While iIndex < aOutputList.Count iCount = 0 sElement = aOutputList[iIndex] While aOutputList.Find(sElement) <> -1 Inc iCount aOutputList.Remove(aOutputList.Find(sElement)) Wend If iCount Mod 2 = 1 Then aOutputList.Add(sElement, iIndex) Inc iIndex Endif Wend Return aOutputList End ' RemoveMultiple(...)
Example 6
This example demonstrates the case that no return value is specified in a function after Return, but the default value (→ 10.5.1.1.1 Notes) is automatically returned:
Public Function ComputeGGT(x As Integer, y As Integer) As GGT ' For (0,0) is the GGT not defined -> return NULL If x = 0 And If y = 0 Then Return ' Return NULL as default value for objects Endif ' Otherwise execute Euclidean algorithm Return hGGT ' With hGGT as a generated GGT object End ' ComputeGGT(..)
You could have written 'Return NULL' in the fourth line - but 'Return' would be sufficient in this case.
Die Anweisung Return oder Return [ Ausdruck ] beendet als Kontroll-Element eine Prozedur oder eine Funktion.
Die Beispiele sollen der Demonstration des Einsatzes des Kontroll-Elements Return oder Return [ Ausdruck ] in unterschiedlichen Anwendungsfällen dienen.
Beispiel 1
Das klassische Beispiel für eine Funktion wie y = f(x) = x² – 3, als eindeutige Abbildung, kennen Sie sicher noch aus der Schulzeit:
Public Function f(x AS Float) AS Float RETURN (x * x) - 3 End ' Function f(x)
mit dem Aufruf in einer anderen Prozedur:
Public Sub Calculate() Dim y as Float y = f(-5.66) … End ' Calculate()
Beispiel 2
Die u.a. Funktion IsIntegerRange(..) gibt in Abhängigkeit vom Argument iPSumme entweder True oder False als Funktionswert zurück:
Private Function IsIntegerRange(iPSumme As Integer) As Boolean If iPSumme > 2147483647 OR If iPSumme < 0 Then btnReset.SetFocus Return False Else Return True Endif ' Partialsumme-Überlauf ? End ' IsIntegerRange(...)
Beispiel 3
Die Prozedur CheckInput(..) kann immer dann eingesetzt werden, wenn für eine Eingabe-Komponente nur Zeichen aus einem vorgegebenen Eingabe-Alphabet akzeptiert werden:
Public Sub txbInputPolynom1_KeyPress() CheckInput("+-,.^0123456789x") End ' txbInputPolynom1_KeyPress() Public Sub CheckInput(sAllowed As String) Select Case Key.Code Case Key.Left, Key.Right, Key.BackSpace, Key.Delete, Key.End, Key.Home, Key.Enter, Key.Return Return Default If Key.Text And If InStr(sAllowed, Key.Text) Then Return Endif End Select Stop Event End ' CheckInput(sAllowed As String)
Bei ausgewählten Tasten und Zeichen aus dem Eingabe-Alphabet wird die Prozedur CheckInput(..) abgebrochen! Über 'Stop Event' werden alle nicht zulässigen Zeichen und Tastendrücke ignoriert.
Beispiel 4
In dem folgenden Quelltextausschnitt wird die Prozedur abgebrochen, wenn ein Fehler beim Anlegen des Verzeichnisses mit dem angegebenen Pfad auftrat, weil die Existenz des Ordners offensichtlich zwingende Voraussetzung für den Programmablauf ist:
Public Sub MyProzedur() … If Not Exist(sSVNPfad) Then Try Mkdir sSVNPfad If Error = True Then Message.Error("Fehler beim Anlegen des Ordners" & sSVNPfad) RETURN Endif ' Error = True? Endif ' Not Exist(SVNPfad? …
Beispiel 5
Als Funktionswert wird eine OutputListe als String-Array zurückgegeben, nachdem in der als Argument übergebenen InputListe (String-Array) alle mehrfach auftretenden gleichen Elemente entfernt wurden:
Public Function RemoveMultiple(aInputList As String[]) As String[] Dim iCount As Integer Dim iIndex As Integer Dim sElement As String Dim aOutputList As String[] aOutputList = aInputList iIndex = 0 While iIndex < aOutputList.Count iCount = 0 sElement = aOutputList[iIndex] While aOutputList.Find(sElement) <> -1 Inc iCount aOutputList.Remove(aOutputList.Find(sElement)) Wend If iCount Mod 2 = 1 Then aOutputList.Add(sElement, iIndex) Inc iIndex Endif Wend Return aOutputList End ' RemoveMultiple(...)
Beispiel 6
Dieses Beispiel demonstriert den Fall, dass in einer Funktion nach Return kein Rückgabewert angegeben wird, sondern automatisch der Default-Wert (→ 10.5.1.1 Hinweise) zurückgegeben wird:
Public Function ComputeGGT(x As Integer, y As Integer) As GGT ' Für (0,0) ist der GGT nicht definiert → Null zurückgeben If x = 0 And If y = 0 Then Return ' Rückgabe Null als Default-Wert bei Objekten Endif ' Sonst euklidischen Algorithmus ausführen Return hGGT ' Mit hGGT als erzeugtes GGT-Objekt End ' ComputeGGT(..)
Sie hätten in der vierten Zeile auch 'Return Null' schreiben können – 'Return' würde aber in diesem Fall ausreichen.