Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "FunctionName = value" and "Return value" in VB.NET?

Tags:

vb.net

In VB.NET functions you can return values in two ways. For example if I have a function called "AddTwoInts" which takes two int variables as parameters, add them together and return the value I could write the function either as one of the following.

1) "Return":

Function AddTwoInts(ByVal intOne As Integer, ByVal intTwo As Integer) As Integer
    Return (intOne + intTwo)
End Function

2) "Function = value":

Function AddTwoInts(ByVal intOne As Integer, ByVal intTwo As Integer) As Integer
    AddTwoInts = (intOne + intTwo)
End Function

My question is this: is there any difference between the two or a reason to use one over the other?

like image 393
Phen Avatar asked Sep 20 '13 12:09

Phen


1 Answers

In your example, there's no difference. However, assignment operator doesn't really exit the function:

Function AddTwoInts(ByVal intOne As Integer, ByVal intTwo As Integer) As Integer
    Return (intOne + intTwo)
    Console.WriteLine("Still alive") ' This will not be printed!
End Function


Function AddTwoInts(ByVal intOne As Integer, ByVal intTwo As Integer) As Integer
    AddTwoInts = (intOne + intTwo)
    Console.WriteLine("Still alive") ' This will  be printed!
End Function

Please don't use the second form, as it is the old language feature inherited from VB6 in order to help migration.

like image 114
Nemanja Boric Avatar answered Nov 05 '22 20:11

Nemanja Boric