views:

4941

answers:

5

In order to return a value from a VB.NET function one can assign a value to the "Functions Name" or use "return value."

I sometimes see these inter-mixed in the same function. Personally, I prefer the return.

My question is, what is the internal difference, if any, between the two?

+1  A: 

Do the following is only provided for vb6 developers to easily port code over:

Public Function MyFunction() As String
    MyFunction = "Hello"
End Function

I would definitly not recommend keeping doing it if your project includes anyone who hasn't worked with vb6, as this syntax will be confusing.

Tom Anderson
why the downvote?
Tom Anderson
I was scratching my head as well... Balancing.
Tomalak
My question is what is about the internal differences between the two, not preferences or best practices.
Some explanation as to *why* you would recommend the one thing over the other would be good, though.
Tomalak
The post also appeared agressive and offensive with the "you" wording. Especially when, given that context, it contradicts my post.
... read "I prefer the return."
Removed last bit as it wasn't needed, thanks for pointing it out, thats what I get for writing when I first wake up! :)
Tom Anderson
+4  A: 

There is probably no difference. IIRC, the compiler generated IL converts them both into Return statements unless there is additional usage of a _returnValue variable.

The readability of the FunctionName assignment is poor in my opinion, and an example of a bad VB6 habit. I prefer the _returnValue (NOT RETVAL) variable method also.

StingyJack
I like returnValue as it is easier to read in my opinion.
Mitchel Sellers
@Mitchel - exactly.
StingyJack
A: 

99 times out of 100 I'll use "return value".

Every once in a while I'll have a function where the other type not only allows me to save a variable declaration, but do it in a way that actually significantly clarifies the function. Usually this happens when I would want to name the return value the same as the function anyway, and often these are recursive functions; something about that construct lends it to the implicit return variable. However, that scenario is extremely rare. I don't know if I have any functions using implicit return variables at all in my current project.

Joel Coehoorn
+4  A: 

Let's take a look... Oddly the "functionName =" generates less IL?

Code:

Public Function Test() As String
    Test = "Test"
End Function


Public Function Test2() As String
    Return "Test"
End Function

IL:

.method public static string Test() cil managed
{
    .maxstack 1
    .locals init (
        [0] string Test)
    L_0000: nop 
    L_0001: ldstr "Test"
    L_0006: stloc.0 
    L_0007: ldloc.0 
    L_0008: ret 
}

.method public static string Test2() cil managed
{
    .maxstack 1
    .locals init (
        [0] string Test2)
    L_0000: nop 
    L_0001: ldstr "Test"
    L_0006: stloc.0 
    L_0007: br.s L_0009
    L_0009: ldloc.0 
    L_000a: ret 
}
TGnat
Interesting: the implicit return saved one instruction.
Joel Coehoorn
Nice post. Why is the branch to L_0009 necessary? Perhaps it is there simply because the return is not optimized away?
Return is compatible with C# is more readable for more programmers and it sounds better
Rulas
Rulas, your comment is irrelevant and unjustified, please read all responses and comments before commenting.
Waves this post saysPublic Function Test() As String Test = "Test"End FunctionPublic Function Test2() As String Return "Test"End FunctionBoth options works fine in VB.NET but first one doesn't work with C# I just want to express that, try to meke it generic
Rulas
waves, the jump will normally be JITed away and is there because there could have been code between the return and the end of the function (and return always generate the same IL it seem, store result and jump to end of method). To be equivalent to the "Return" case the Test method should include an "Exit Sub" statement.
VirtualBlackFox
It should be noted that this is only in debug mode (thus, irrelevant)! In release mode, the same code is generated.
Konrad Rudolph
+1  A: 

The difference is that they DO DIFFERENT THINGS!

'Return value' does 2 things:
1. It sets the function return value at that point 2. It immediately exits the function

No further code in the function executes!

'Functionname = value' does 1 thing: 1. It sets the function return value at that point

Other code in the function continues to execute This enables additional logic to refine or override the function return value

Huge difference folks. Remember it's not all about state, it's also about flow.

noeldp