Category: VB

VB.NET: Return vs Exit Sub

The other day I was suddenly wondering which one is better for exiting a Sub, return or exit?

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. See the discussion here.

PS. IL is an assembly language usually used to debug .NET code at low level. See here for more.

VBScript: “functionName = value” vs. “return value”

In VB.NET, it’s quite common to see people returning value for a function by assigning value to the function name:

Function GetNumber as Integer
  GetNumber = 15
End Function

This is equivalent to:

Function GetNumber as Integer
  return 15
End Function

The difference between this 2 methods is that if you assign a value to the function name, the function does not exit until it hits the end of the function or hits the Exit Function statement, where if you return the value, the function exits at that point.

Assigning value to function name is a legacy of the VB world and using the return statement is more conventional.

Check out more details here: