writing a Sub versus writing a Function with no return value

  • Thread starter Thread starter Mark Kamoski
  • Start date Start date
M

Mark Kamoski

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.
 
Mark Kamoski said:
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
 
Mark Kamoski said:
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?
 
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
 
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
 
Back
Top