Return Statement

Moves control back to the code that called a procedure.

Syntax

Return
or
Return expression

Parts

expression
The value to return to the caller.
Mandatory for all procedures of the types Function and Get, and procedures Method and Sub with a return type.

Instructions

In all procedures of the type Set and procedures of Method and Sub without a return type, Return is the same as Exit Property, Exit Method or Exit Sub. You must not supply expression.

In all procedures of the types Function and Get and procedures of Method and Sub with a return type, Return must include expression. The data type of expression must be compatible with the return type of the procedure. As an alternative, you can assign expression to the name of the procedure, then use Exit Function, Exit Property, Exit Method, or Exit Sub.

If you put Return in a block of the types Try or Catch that also includes a block Finally, Return causes Finally to run. And if Return includes expression, it gets calculated after End Try. Return is not permitted in blocks of the type Finally.

Examples

Method RelativeTime(sec As Int32) As String
    If sec < 2*60 Then Return "seconds"
    If sec < 2*60*60 Then Return "minutes"
    If sec < 2*24*60*60 Then Return "hours"
    If sec < 2*7*24*60*60 Then Return "days"
    If sec < 2*30*24*60*60 Then Return "weeks"
    If sec < 2*365*24*60*60 Then Return "months"
    Return "years"
End Method

See also