473,461 Members | 1,681 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 20909
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tung Wai Yip | last post by:
I've build the following utility. It works so far but I want to make sure I'm doing the right thing. I am running Python 2.3 on windows 2000. def execute(cmd): """ execute cmd in sub-process,...
27
by: Maximus | last post by:
Hi, I was just wondering, is it good to use return without arguments in a void function as following: void SetMapLayer() { if( !Map ) return; layer = LAYER_MAP; }
4
by: John Black | last post by:
Hi, I wonder what is the difference between ending the main() with and without exit(0)? My code is like this flow, class ClassA{ ClassB* cb; int toExit();
0
by: Paul Patrick | last post by:
Keep getting this error when I input the following command gcj -o filename.java --main=filename filename.java Any ideas on what the problem might be?
11
by: Peter Steele | last post by:
I have a Windows application written in C# that I want to return a non-zero exit code when it it run. The problem is that as a Windows application, there doesn't seem to be a way to control this....
3
by: John Williams | last post by:
VB .Net 2003, .Net Framework 1.1, Windows XP Pro SP1 I've written a console application which I run as a scheduled task. How do I return an exit status value (or string) from the application so...
4
by: Wardeaux | last post by:
Hope this is easy... I have a simple winform app that does some work and then exits... I need to return a value to the calling app/process to indicate completion status... what's the...
20
by: lovecreatesbeauty | last post by:
Hello experts, Is the following code snippet legal? If it is, how can exit() do the keyword return a favor and give a return value to the main function? Can a function call (or only this...
3
by: asearle | last post by:
Hallo everyone, I have a perl script which can return error codes. I have done this with, for example, 'exit 11;', which would (I imagine) send the exit code 11 (instead of 0) back to the calling...
11
by: =?Utf-8?B?U3RldmVEQjE=?= | last post by:
Hi all. I'm using VS 2008 Express C++. I created a console application back in 1999, and updated it for VC++ 6.0 in 2001. I've updated again this past month, and have found enough differences...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.