SgDotNet
Singapore Professional .NET User Group -For Cool Developers

Sub vs. Functions in VB.NET

Latest post 01-05-2006 3:46 PM by feelite. 8 replies.
  • 01-29-2005 3:40 PM

    • Apex
    • Top 150 Contributor
    • Joined on 01-17-2005
    • Posts 13

    Sub vs. Functions in VB.NET

    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?
  • 01-29-2005 4:24 PM In reply to

    Re: Sub vs. Functions in VB.NET

     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?


    I guessed it is VB.NET's way of being more elaborative and specific. You have already mentioned the difference, Sub does not return a value (although still can be done) and Function does.

    But I'm curious on what you mentioned, can you actually code a function without a return type in VB.NET?
    Software development made easy with Paladin RAD Framework. Save some trees, use Stickies.NET
  • 01-30-2005 8:16 PM In reply to

    Re: Sub vs. Functions in VB.NET

     Firedancer wrote:

    But I'm curious on what you mentioned, can you actually code a function without a return type in VB.NET?


     Yes. It will return Object if no type is specified.
    I think in the older days of VB, if no type is specified, Variant is assumed to be the default?

    Function FuncNoRetType()
       Return 10
    End
    Function

    Corresponding IL code
    .method public static object  FuncNoRetType() cil managed
    {
      .maxstack  1
      .locals init ([0] object FuncNoRetType)
      IL_0000:  nop
      IL_0001:  ldc.i4.s   10
      IL_0003:  box        [mscorlib]System.Int32
      IL_0008:  stloc.0
      IL_0009:  br.s       IL_000b
      IL_000b:  ldloc.0
      IL_000c:  ret
    }
    Notice the boxing of 10 to a Int32.

    But if

    Function
    FuncNoRetType()
     
    ' do nothing
    End
    Function

    .method public static object  FuncNoRetType() cil managed
    {
      .maxstack  1
      .locals init ([0] object FuncNoRetType)
      IL_0000:  nop
      IL_0001:  ldloc.0
      IL_0002:  ret
    }

  • 01-30-2005 8:50 PM In reply to

    Re: Sub vs. Functions in VB.NET

    As far as I can remember, there is also a preformance difference between a sub and a function...[*-)]

    Blog -> http://www.dotnetjunkies.com/weblog/rohanthomas/ Singapore's Professional .NET User Group Rocks!!!! Yes
  • 02-08-2005 9:06 PM In reply to

    Re: Sub vs. Functions in VB.NET

    Wouldn't matter much in the performance difference nowadays, with all those super fast servers out there.
    Just plain 'olde me.
  • 02-09-2005 4:01 PM In reply to

    Re: Sub vs. Functions in VB.NET

    all those.. yes...
    But, not all...[*-)]
    Anyways, in critical sitiation it might make a difference...
    Blog -> http://www.dotnetjunkies.com/weblog/rohanthomas/ Singapore's Professional .NET User Group Rocks!!!! Yes
  • 02-09-2005 5:39 PM In reply to

    Re: Sub vs. Functions in VB.NET

    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)

  • 01-05-2006 3:01 PM In reply to

    • Lavanyaa
    • Top 150 Contributor
    • Joined on 12-30-2005
    • Singapore
    • Posts 10

    Re: Sub vs. Functions in VB.NET

    Performance will be better while using sub compared to function.

  • 01-05-2006 3:46 PM In reply to

    Re: Sub vs. Functions in VB.NET

     

    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

     

    Public Sub NoReturnSubVB()

    End Sub

    .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 void NoReturnCSharp()

    {

    }

    .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.

     

    http://feelite.com/blog
Page 1 of 1 (9 items) | RSS
Copyright SgDotNet 2004-2008
Powered by Community Server (Commercial Edition), by Telligent Systems