Connecting Tech Pros Worldwide Forums | Help | Site Map

writing a Sub versus writing a Function with no return value

Mark Kamoski
Guest
 
Posts: n/a
#1: Nov 19 '05

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.



Jeremy Cowles
Guest
 
Posts: n/a
#2: Nov 19 '05

re: writing a Sub versus writing a Function with no return value


"Mark Kamoski" <mkamoski@yahoo.com> wrote in message
news:%23K$19RiRDHA.2768@tk2msftngp13.phx.gbl...[color=blue]
>
> Hi Everyone.
>
> What is the real difference between writing a Sub versus writing a[/color]
Function[color=blue]
> 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.
>[/color]

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



Armin Zingler
Guest
 
Posts: n/a
#3: Nov 19 '05

re: writing a Sub versus writing a Function with no return value


"Mark Kamoski" <mkamoski@yahoo.com> schrieb
[color=blue]
> 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.[/color]

What is a function without a return value? Example?


--
Armin

Jay B. Harlow [MVP - Outlook]
Guest
 
Posts: n/a
#4: Nov 19 '05

re: writing a Sub versus writing a Function with no return value


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" <mkamoski@yahoo.com> wrote in message
news:%23K$19RiRDHA.2768@tk2msftngp13.phx.gbl...[color=blue]
>
> Hi Everyone.
>
> What is the real difference between writing a Sub versus writing a[/color]
Function[color=blue]
> 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.
>
>[/color]


Kim Mitchell
Guest
 
Posts: n/a
#5: Nov 19 '05

re: writing a Sub versus writing a Function with no return value


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" <mkamoski@yahoo.com> wrote in message
news:%23K$19RiRDHA.2768@tk2msftngp13.phx.gbl...[color=blue]
>
> Hi Everyone.
>
> What is the real difference between writing a Sub versus writing a[/color]
Function[color=blue]
> 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.
>
>[/color]


Closed Thread


Similar Visual Basic .NET bytes