Return Statement


Return Statement

Syntax

In a subroutine or Set property accessor:

     Return  

In a function or property Get accessor:

     Return returnValue 


returnValue (required; any)

The return value of the function

Description

The Return statement returns control to the calling procedure from a sub procedure, property, or function. When used from functions and property Get accessors, it also returns an associated value to the calling procedure.

Usage at a Glance

  • If the Return statement appears in a function or in the Get component of a property, it must specify a return value.

  • Return causes program flow to leave the active procedure and return to the calling procedure; any statements in the function or subroutine that follow Return are not executed.

  • Return is identical in operation to the Exit Sub statement; it prematurely transfers control from a procedure to the calling routine. It is also similar to the Exit Function statement; while it prematurely transfers control out of the function, it also allows a particular value to be returned by the function.

Example

     Public Sub AddAndDisplayNumbers(  )        Dim returnedValue As Double = GetNumbers(  )        MsgBox("The sum of values is " & returnedValue)     End Sub     Public Function GetNumbers(  ) As Double        ' ----- Prompts for up to 10 numbers and returns the sum.        Dim counter As Integer = 1        Dim userInput As String        Dim sumOfNumbers As Double = 0#        Do           ' ----- Get the number from the user.           userInput = InputBox("Enter number " & counter & ":", "Sum")           If (userInput = "") Then Exit Do           ' ----- Check for valid input.           If IsNumeric(userInput) Then              sumOfNumbers += CDbl(userInput)              counter = counter + 1           End If        Loop While (counter <= 10)        ' ----- All added and ready to return.        Return sumOfNumbers     End Function 

Version Differences

In VB 6, the Return statement was used in a block of code accessed through a GoSub statement. The GoSub statement is no longer supported in VB under .NET, and the Return statement now serves a different purpose.

See Also

Exit Statement




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net