Apex wrote:What's the difference between a sub and a function? If I write a function that doesn't return a value, isn't it just like a sub? Why are both available when a function can do everything itself? Is there any difference at the IL level?
Firedancer wrote:But I'm curious on what you mentioned, can you actually code a function without a return type in VB.NET?
As far as I can remember, there is also a preformance difference between a sub and a function...
VB.net is not always used for programming web or win app... dun forget you still got compact framework, which runs a bit slower...
Best Regards, Kit Kai, MVP (SharePoint Portal Server)
Performance will be better while using sub compared to function.
Public Function NoReturnFunVB()
End Function
.method public instance object NoReturnFunVB() cil managed{ // Code size 3 (0x3) .maxstack 1 .locals init ([0] object NoReturnFunVB) IL_0000: nop IL_0001: ldloc.0 IL_0002: ret} // end of method Class1::NoReturnFunVB
.method public instance void NoReturnSubVB() cil managed{ // Code size 3 (0x3) .maxstack 8 IL_0000: nop IL_0001: nop IL_0002: ret} // end of method Class1::NoReturnSubVB
public
{
}
.method public hidebysig instance void NoReturnCSharp() cil managed{ // Code size 2 (0x2) .maxstack 0 IL_0000: nop IL_0001: ret} // end of method Class1::NoReturnCSharp
Look at these MSIL instructions, C# has the cleanest implementation of a void method; the size of the evaluation stack is 0 and there is nothing except a mandatory nop, followed by return.
VB.NET sub is next, but mysteriously it has a .maxstack 8 statement at the top, what are they thinking?? Although that does not mean the run time evaluation stack is 8, the statement is used only for static analyzer by the verifier during the assembly loading phase.
VB.NET function with no explicit return type is the ugliest. To preserve genericity, the all mighty object is used as the base type so that even if you return an arraylist of arraylists of arraylists of... is fine.