By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,537 Members | 926 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,537 IT Pros & Developers. It's quick & easy.

writing a Sub versus writing a Function with no return value

P: n/a

Hi Everyone.

What is the real difference between writing a Sub versus writing a Function
with no return value?

It seems to me that both of these would need to compile to the same IL, so
it seems they are identical.

Please advise.

Thank you.

--Mark.
Nov 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Mark Kamoski" <mk******@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

Hi Everyone.

What is the real difference between writing a Sub versus writing a Function with no return value?

It seems to me that both of these would need to compile to the same IL, so
it seems they are identical.


When you write code, do you write it based on how you know the IL will
result? The goal is to make your code easier to understand, if the resulting
IL is the same either way, that just makes a better argument to use the
proper mechanisms that are provided by the language. In some cases, you
write your code around IL because you know it will be mucked up other wise
(such is the case of constructors and private field initialization).

Why would you want to only use Functions? This is a step backwards. Here is
another question - Why use Properties if it is only a thin wrapper for a
public field (a property with no validation or logic)? Why use an event when
you can just use a manual Callback via the Win32 API? Why use strong
variable types? Why use Classes & objects, why not just write purely
procedural code? After all IL is just one long list of function calls right?

HTH,
Jeremy

Nov 19 '05 #2

P: n/a
"Mark Kamoski" <mk******@yahoo.com> schrieb
What is the real difference between writing a Sub versus writing a
Function with no return value?

It seems to me that both of these would need to compile to the same
IL, so it seems they are identical.

Please advise.


What is a function without a return value? Example?
--
Armin

Nov 19 '05 #3

P: n/a
Mark,
As Herfried suggests, have you looked at the code with ildasm.exe?

They are not identical, there is overhead in the function for the return
value, although you never explicitly use or set the return value, VB.NET
implicitly sets the return value.

Further I totally agree with Jeremy. Write code that indicates the intent.
If you have a 'function' that does not return a value, then make it a sub.
In 6 months you will be happier, and any one who takes over you code will be
happier. Otherwise someone may be spending the day, cursing your name 'This
routine never returns a value, what value should we be returning' :-)

Just a thought
Jay

"Mark Kamoski" <mk******@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

Hi Everyone.

What is the real difference between writing a Sub versus writing a Function with no return value?

It seems to me that both of these would need to compile to the same IL, so
it seems they are identical.

Please advise.

Thank you.

--Mark.

Nov 19 '05 #4

P: n/a
The other answers were good, but they weren't to the point. The real
difference between a sub and a function is that a function has a return
value and a sub does not. Functions are called as below:

myVar = myFunction(myArgumentList)
(ex. myInt = myIntCalc(Princ, Rate, Time)

where the return value is of the same type as myVar. Subs are called thus:

mySub(myArgumentList)
(ex. LoadTreeView(myNodes,myArr))

The argumentlist may contain variables that pass infomation between the
calling sub and the function or sub but these are not considered return
values. HTH

Kim

"Mark Kamoski" <mk******@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

Hi Everyone.

What is the real difference between writing a Sub versus writing a Function with no return value?

It seems to me that both of these would need to compile to the same IL, so
it seems they are identical.

Please advise.

Thank you.

--Mark.

Nov 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.