Return vs Exit Sub

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

Is there any difference between Return and Exit Sub?

I have some code that uses both when I have an error in my Sub.

Thanks,

Tom
 
Hello tshad,

There is no difference in the IL output from "Return" and "Exit Sub" when
used in a sub. However, for readability I would suggtest using Exit Sub.
Return denotes pushing a value back to the caller, which since a sub doesnt
have a return value, makes for a less clear intent.

-Boo
 
GhostInAK said:
There is no difference in the IL output from "Return" and "Exit Sub" when
used in a sub. However, for readability I would suggtest using Exit Sub.
Return denotes pushing a value back to the caller, which since a sub doesnt
have a return value, makes for a less clear intent.

Well, you could interpret 'Return' either as "Return something" or "Return
to something".
 
Herfried,
Well, you could interpret 'Return' either as "Return something" or "Return
to something".
It is not about holidays.

It is about a nothing returning method in VBNet what has the name Sub.

In far past when stacks did not yet be used, there was set a return address
mostly as last command in a method.

I never investigated this but in a normal modern system, the sub ends and
the program goes on in the one higher stack; one position after where it was
calling the Sub.

:-)

Cor
 
Cor Ligthert said:
Herfried,

It is not about holidays.

It is about a nothing returning method in VBNet what has the name Sub.

In far past when stacks did not yet be used, there was set a return address
mostly as last command in a method.

I never investigated this but in a normal modern system, the sub ends and
the program goes on in the one higher stack; one position after where it was
calling the Sub.

There is an implicit return at the end of a Sub. As far as using Exit Sub
or Return, I have switched to Return as most other languages use Return for
this purpose. VB was the only language to not use return in this manner
because the BASIC return statement indicated the return from a GOSUB
statement.

Mike.
 
Boo,
I view Return as returning to whence I came. Which as Michael suggests is
common across most other languages.

Optionally a value may be included when I return to whence I came.

Unfortunately I am inconsistent on always using Return; I occasionally use
Exit Sub... Which could be considered an odd-ball solution...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Hello tshad,
|
| There is no difference in the IL output from "Return" and "Exit Sub" when
| used in a sub. However, for readability I would suggtest using Exit Sub.
| Return denotes pushing a value back to the caller, which since a sub
doesnt
| have a return value, makes for a less clear intent.
|
| -Boo
|
| > Is there any difference between Return and Exit Sub?
| >
| > I have some code that uses both when I have an error in my Sub.
| >
| > Thanks,
| >
| > Tom
| >
|
|
 
Michael,

VBNet is the only language where I am/was easily able to go deeper than 8
levels. That because of the nice descriptions of the levels.

:-)

Cor

Michael D. Ober said:
Cor Ligthert said:
Herfried,

It is not about holidays.

It is about a nothing returning method in VBNet what has the name Sub.

In far past when stacks did not yet be used, there was set a return address
mostly as last command in a method.

I never investigated this but in a normal modern system, the sub ends and
the program goes on in the one higher stack; one position after where it was
calling the Sub.

There is an implicit return at the end of a Sub. As far as using Exit Sub
or Return, I have switched to Return as most other languages use Return
for
this purpose. VB was the only language to not use return in this manner
because the BASIC return statement indicated the return from a GOSUB
statement.

Mike.
 
Jay,

Have a look at my reply to Michael, otherwise I would agree with you.

:-)

Cor
 
Cor,

I assume you mean

Exit For
Exit Do
Exit ...

Yes, this is nice, but when I always comment the inner exits. Otherwise,
they are tough to track.

Mike.
 
Hello Herfried K. Wagner [MVP],

While one could interpret the meaning in both ways when used in a sub.. Why
cause the confusion? In a sub use Exit Sub and in a function use Return.
Then there is no ambiguity. Yes, we all know it's equivalent if we bothered
to read the documentation (everyone's F1 key borke?)
ms-help://MS.VSCC.2003/MS.MSDNQTR.2005APR.1033/vblr7/html/vastmReturn.htm

Just because you can do a thing doesn't mean you should do a thing.

-Boo
 
GhostInAK said:
Hello tshad,

There is no difference in the IL output from "Return" and "Exit Sub" when
used in a sub. However, for readability I would suggtest using Exit Sub.
Return denotes pushing a value back to the caller, which since a sub
doesnt have a return value, makes for a less clear intent.

Is that similar to

Function DoSomething () as Boolean
if something then
DoSomething = true
else
DoSomething = false
end if
End Function

or

Function DoSomething () as Boolean
if something then
return true
else
return = false
end if
End Function

or

Function DoSomething () as Boolean
Dim i as Boolean
if something then
i = true
else
i = false
end if
return i
End Function

I was never sure which was best as they all work and I tend to do it all
three ways.

6 of one...

Tom
 
IMO, the first one is bad. I always hated that construct since it looks like
a normal variable assignment and it makes the code a lot harder to
understand (I guess they kept it in for compatibility reasons). A simple
return is clear and easy to understand. As for the other question of Exit
Sub vs Return in subs I tend to use return since it is what both C++ and C#
use and I think it looks better.

/claes
 
Tshad,

This is a function which should always return something or nothing and than
exit.

In your first sample you could tend for a exit function, I never use it like
that, I will forever use it as in your second sample.

The main difference between a Sub and a Function is that a Sub does not
return something or nothing.

Now I write this, for me is that an extra reason to use Exit Sub in a Sub.

Cor
 
Cor Ligthert said:
Tshad,

This is a function which should always return something or nothing and
than exit.

In your first sample you could tend for a exit function, I never use it
like that, I will forever use it as in your second sample.

I tend to use 2 or 3, but I might use 1 if I don't want to create a separate
variable just to use as a return value.

Tom
 
Back
Top