Return
 
Control flow statement to return from a procedure or Gosub

Syntax

Return [ return value ]

Description

Return is used to return control back to the statement immediately following a previous GoSub call. When used in combination with GoSub, no return value can be specified.

Return can also be used inside a Sub or Function to exit the Sub or Function. In a Sub, Return cannot specify a return value. In a Function, Return must specify its return value. It is equivalent to the Function = return value : Exit Function idiom.

A GoSub call must always have a matching Return statement, to avoid stack corruption. Thus, it is often preferable to use a Sub rather than GoSub/Return.

When compiling with in the -lang qb dialect, Return can only be used to return from a GoSub.

Example

'' Compile with -lang qb
Print "Let's Gosub!"
GoSub MyGosub
Print "Back from Gosub!"
Sleep
End

MyGosub:
Print "In Gosub!"
Return


Type rational_number                   '' simple rational number type
   numerator As Integer
   denominator As Integer
End Type

Type rational As rational_number       '' type alias for clearer code

'' multiplies two rational types (note: r1 remains unchanged due to the BYVAL option)
Function rational_multiply( r1 As rational, r2 As rational ) As rational

   r1.numerator *= r2.numerator        '' multiply the divisors ...
   r1.denominator *= r2.denominator
   Return r1                           '' ... and return the rational

End Function

Dim As rational r1 = ( 6, 105 )        '' define some rationals r1 and r2
Dim As rational r2 = ( 70, 4 )
Dim As rational r3

r3 = rational_multiply( r1, r2 )       '' multiply and store the result in r3

'' display the expression (using STR to eliminate leading space when printing numeric types)
Print Str( r1.numerator ) ; "/" ; Str( r1.denominator ) ; " * " ;
Print Str( r2.numerator ) ; "/" ; Str( r2.denominator ) ; " = " ;
Print Str( r3.numerator ) ; "/" ; Str( r3.denominator )

Sleep 
End 0


Dialect Differences

  • In the -lang qb dialect, Return can be used only to return from a GoSub
  • In the -lang depracted and -lang fb dialects, Return can be used only to set the return value of a function and exit it.

Differences from QB

See also