King Jeremy
King Jeremy

Reputation: 33

usage of return

I've seen a code where return is used like below

If myFunction() Then Return

Private Function myFunction() As Boolean
   Try //something
      return true
   Catch
      return false
   End Try
End Function

I didn't quite get the logic with the "If myFunction() Then return" Any explanation would be much appreciated.

Upvotes: 2

Views: 1484

Answers (3)

Marlon
Marlon

Reputation: 20322

Return means to exit the function which returns no value. It's equivalent of Exit Sub in VB6 (if you are familiar with the language).

For example, if I have the following code:

Sub Foo()
    If True Then Return
    MessageBox.Show("Hello World");
End Sub

The message box will never show because Return exits the function.

For your case, let's replace If True Then Return with If myFunction() Then Return:

Sub Foo()
    If myFunction() Then Return
    MessageBox.Show("Hello World");
End Sub

Function myFunction As Boolean
    Try 'something
        Return True
    Catch
        Return False
    End Try
End Function

If myFunction returns true, then the message box will not show. If myFunction fails, then the message box will show.

Upvotes: 3

GilliMonsta
GilliMonsta

Reputation: 369

If myFunction returns true, then it will return. If myFunction returns false, then it will continue.

I assume in your example that the If statement is out of context (and is actually part of some other Function or subroutine).

Upvotes: 1

Konstantin Weitz
Konstantin Weitz

Reputation: 6432

The If statement works the following way

If 'condition' Then 'do something'

'condition' can either be true or false. If it is true, 'do something' will be executed. myFunction() returns such a true/false value (called Boolean), you can thus write it in the spot of the 'condition'.

Upvotes: 2

Related Questions