Syntax: "Exit Sub" or "Return" in VB.NET subroutines

vb.netSyntaxReturnSubroutine

vb.net Problem Overview


Both "Exit Sub" or "Return" seem to accomplish the same thing -- exit a subroutine. Is there any difference in how they work under the covers?

That is,

Private Sub exitNow()
    Exit Sub
End Sub

or

Private Sub exitNow()
    Return
End Sub

vb.net Solutions


Solution 1 - vb.net

From the doc:

> In a Sub or Set procedure, the Return statement is equivalent to an Exit Sub or Exit Property statement, and expression must not be supplied.

So they're the same in this context.

(Return (<value>) is used in functions and property.get's. Obviously slightly different in that context).

Solution 2 - vb.net

I tend to prefer Return over Exit Sub. Because once in a while you change from Sub to Function. In this case Exit Sub could be converted to Exit Function, but this assumes that there was a previous assignment to the function name (alike VB 6), which most probably didn't happen. Return would catch this situation - if the method should return a value, Return with no argument will fail at compile time.

Solution 3 - vb.net

If you inspect the IL output of the 2 statements, they are the same. However, since ’return’ is meant for pushing something back to the caller, so strictly speaking, ‘Exit Sub’ is more suitable for using in a Sub.

Solution 4 - vb.net

They are the same in this context.

However, from the code readability point of view, "Exit Sub" would be clearer since the "Return" indicates that something some value is being used as an output (which is not the case with Sub-routines).

Solution 5 - vb.net

  • First of all, Procedures comes with sub, we should know that we are working on specific procedures that don't return a specific value with the ability of passing some specific parameters or even without passing any parameter. Such as:
  • Print something().
  • Calculate the factorial of integer number CalcFact(X).
  • Do some processes for a specific task.
  • Function is a specific process programmed to achieve a specific task by also passing some specific parameters, and it has to return some value that can be used to to complete the overall task, such as validation the user name and user pass.

In short Sub Doesn't return value and we call it directly "Print HelloWorld()" , whereas functions do such as:

  • ValidUsersNameAndPass("Johne","jOhNe13042019") ' This could return a Boolean value.
  • ValidUsersNameAndPass("Johne","jOhNe13042019"); // This could return a Boolean value.

Solution 6 - vb.net

I wanted to confirm that they act the same in lambda expressions too, and they do:

Sub test()

    Dim a As Action = Sub() Exit Sub
    Dim b As Action = Sub() Return

    a()
    b()

    MsgBox("Yes they do!") 
 End Sub

Solution 7 - vb.net

While there are exceptions like guard clauses, in most cases I would consider either a sign that the method is too long.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeffView Question on Stackoverflow
Solution 1 - vb.netRBarryYoungView Answer on Stackoverflow
Solution 2 - vb.netMikeView Answer on Stackoverflow
Solution 3 - vb.netdipan chikaniView Answer on Stackoverflow
Solution 4 - vb.netThiago BurgosView Answer on Stackoverflow
Solution 5 - vb.netMAMPROView Answer on Stackoverflow
Solution 6 - vb.netSlaiView Answer on Stackoverflow
Solution 7 - vb.netJoel CoehoornView Answer on Stackoverflow