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

7

68

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
Phuongphycology answered 17/6, 2009 at 1:20 Comment(0)
H
79

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).

Hellhole answered 17/6, 2009 at 1:23 Comment(0)
P
25

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.

Petroglyph answered 19/4, 2017 at 16:27 Comment(1)
Good insight. Thanks Mike!Phuongphycology
J
7

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.

Jennee answered 25/9, 2012 at 5:44 Comment(3)
Actually return has two meanings. For functions it means returning something back to the caller, and in both functions and subs it means returning execution back to the calling location. This is the case in most languages with a "return" keyword. "Exit Sub" only exists for backwards compatibility with pre-.NET VB.Phiphenomenon
I don't agree with "‘Exit Sub’ is more suitable for using in a Sub." There is nothing wrong with Exit Sub, HOWEVER it is specific to Visual Basic; return is used to exit from subroutines in all languages inspired by C: C#, java, C++, Objective-C.Indicative
@Indicative I agree with "‘Exit Sub’ is more suitable for using in a Sub because we are using VB.Net language and this language is more user friendly then other so Exist sub is best option then return because you can check other keywords like IF END IF etc. I hope you understood what I mean.Anarchic
S
4

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).

Semiquaver answered 4/2, 2014 at 14:27 Comment(2)
This should be a comment, rather than an answer.Colincolinson
I don't agree with "‘"Exit Sub" would be clearer.." There is nothing wrong with Exit Sub, HOWEVER it is specific to Visual Basic; return is used to exit from subroutines in all languages inspired by C: C#, java, C++, Objective-C. Downvoted because this says the same thing as chikani's answer two years earlier, so adds nothing to the discussion.Indicative
S
1
  • 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.
Scoot answered 12/4, 2019 at 21:28 Comment(0)
C
0

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
Coloratura answered 18/7, 2016 at 18:52 Comment(4)
what is this? This seems like a jokeBusinesswoman
@CengizAraz as in this doesn't seem like real code that would compile? msdn.microsoft.com/en-us/library/bb531253.aspxColoratura
@Patrick the inferred type in VS 2015 is anonymous delegate, but I am not sure if there are any differences with Action.Coloratura
This code adds nothing to the discussion. What do you believe it demonstrates?Indicative
U
-1

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

Univocal answered 17/6, 2009 at 1:24 Comment(2)
I think that either would be very useful in recursion.Triny
This is not an answer to the question.Indicative

© 2022 - 2024 — McMap. All rights reserved.