468,738 Members | 1,792 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,738 developers. It's quick & easy.

Return vs Exit Sub

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
Jul 21 '06 #1
14 20434
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

Jul 21 '06 #2
Tshad,

There should be not any difference although I prefer Exit Sub.

Cor

"tshad" <ts**********@ftsolutions.comschreef in bericht
news:uR**************@TK2MSFTNGP05.phx.gbl...
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

Jul 21 '06 #3
"GhostInAK" <gh*******@gmail.comschrieb:
>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".

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Jul 21 '06 #4
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

"Herfried K. Wagner [MVP]" <hi***************@gmx.atschreef in bericht
news:eF**************@TK2MSFTNGP02.phx.gbl...
"GhostInAK" <gh*******@gmail.comschrieb:
>>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".

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Jul 21 '06 #5

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
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.
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.
>
:-)

Cor

"Herfried K. Wagner [MVP]" <hi***************@gmx.atschreef in bericht
news:eF**************@TK2MSFTNGP02.phx.gbl...
"GhostInAK" <gh*******@gmail.comschrieb:
>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".

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>




Jul 21 '06 #6
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
"GhostInAK" <gh*******@gmail.comwrote in message
news:c7**************************@news.microsoft.c om...
| 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
| >
|
|
Jul 21 '06 #7
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" <obermd.@.alum.mit.edu.nospamschreef in bericht
news:V6****************@newsread3.news.pas.earthli nk.net...
>
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>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.

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

Cor

"Herfried K. Wagner [MVP]" <hi***************@gmx.atschreef in bericht
news:eF**************@TK2MSFTNGP02.phx.gbl...
"GhostInAK" <gh*******@gmail.comschrieb:
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".

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>





Jul 21 '06 #8
Jay,

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

:-)

Cor

"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.netschreef in
bericht news:uX**************@TK2MSFTNGP02.phx.gbl...
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
"GhostInAK" <gh*******@gmail.comwrote in message
news:c7**************************@news.microsoft.c om...
| 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
| >
|
|


Jul 21 '06 #9
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.

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uK**************@TK2MSFTNGP05.phx.gbl...
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" <obermd.@.alum.mit.edu.nospamschreef in bericht
news:V6****************@newsread3.news.pas.earthli nk.net...
>>
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>>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.

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

Cor

"Herfried K. Wagner [MVP]" <hi***************@gmx.atschreef in bericht
news:eF**************@TK2MSFTNGP02.phx.gbl...
"GhostInAK" <gh*******@gmail.comschrieb:
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".

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>




Jul 21 '06 #10
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" <gh*******@gmail.comschrieb:
>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".

Jul 21 '06 #11
"GhostInAK" <gh*******@gmail.comwrote in message
news:c7**************************@news.microsoft.c om...
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
>
-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


Jul 24 '06 #12
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
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
>>
-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



Jul 25 '06 #13
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

"tshad" <ts**********@ftsolutions.comschreef in bericht
news:%2****************@TK2MSFTNGP03.phx.gbl...
"GhostInAK" <gh*******@gmail.comwrote in message
news:c7**************************@news.microsoft.c om...
>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
>>
-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



Jul 25 '06 #14
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:%2***************@TK2MSFTNGP05.phx.gbl...
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
>
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

"tshad" <ts**********@ftsolutions.comschreef in bericht
news:%2****************@TK2MSFTNGP03.phx.gbl...
>"GhostInAK" <gh*******@gmail.comwrote in message
news:c7**************************@news.microsoft. com...
>>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
>>>
-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



Jul 25 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

27 posts views Thread by Maximus | last post: by
4 posts views Thread by John Black | last post: by
11 posts views Thread by Peter Steele | last post: by
20 posts views Thread by lovecreatesbeauty | last post: by
3 posts views Thread by asearle | last post: by
11 posts views Thread by =?Utf-8?B?U3RldmVEQjE=?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.