User Tools

Site Tools


k10:k10.5:k10.5.1:start

10.5.1 Return

The statement Return or Return[ expression] terminates a procedure or function as a control element.

10.5.1.1.1 Notes

  • You can only end a procedure with Return. A return value (expression) must not be specified.
  • However, if you end a function with 'Return Expression', the expression is returned as a function value. If you terminate a function without specifying an expression, the default value (→ chapter 7.0 Data Types) for the specified function value data type is returned as the function value.
  • Since Gambas version 3.1, Return is also used to return a GoSub instruction.

10.5.1.2 Examples

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.

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k10/k10.5/k10.5.1/start.txt · Last modified: 23.09.2023 by honsek

Page Tools