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

In VB.NET functions, you can return values ​​in two ways. For example, if I have a function called "AddTwoInts" that takes two int variables as parameters, add them together and return a value that I could write to 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 a difference between the two or a reason for using one over the other?

+4
source share
2 answers

There is no difference in your example. However, the assignment operator does not 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 do not use the second form, as this is a function of the old language inherited from VB6 to help migration.

+10
source

In your example, there is no difference between the two. The only real reason for choosing the first is that it is similar to other languages. Other languages ​​do not support the second example.

As already indicated, assigning a function name does not cause a return from the function.

The IL generated by the two examples will be identical.

0
source

All Articles