Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between using a Return statement compared to assigning a value to the function?

Tags:

vb.net

I'm just going through our codebase at work, trying to clear the Warnings that get generated when compiled and there are a ton of "Function without As clause, clause" warnings.

While going back and changing most of these to be a Sub instead of a Function, I occasionally overlook a Return statement because I just highlight the method name and look for a value assigned to it, which is how it's done in most of the code.

So I was just wondering if there's any difference between these:

Private Function Foo() As String
    Foo = String.Empty
End Function

Or:

Private Function Foo() As String
    Return String.Empty
End Function

Functionally, it seems to be identical but I wasn't sure if there was any behind the scenes differences in regards to how the compiler interprets this.

like image 353
sab669 Avatar asked Aug 17 '15 12:08

sab669


1 Answers

The main difference is that Return exits the function while assigning a value to the function name does not. So the two equivalents should be

Private Function Foo() As String
    Foo = String.Empty
    Exit Function
    '.....
End Function

Or:

Private Function Foo() As String
    Return String.Empty
    '.....
End Function
like image 76
Blackwood Avatar answered Nov 11 '22 08:11

Blackwood