Purpose of using sub routines over functions

后端 未结 7 1170
抹茶落季
抹茶落季 2021-02-07 07:38

I\'ve been working with Access for a while now, and although I understand the obvious benefit of a Function over a Sub, been that it can return values as a result, I\'m not sure

相关标签:
7条回答
  • 2021-02-07 08:16

    The main difference is not only the return value, it seems that subs are faster than functions (at least in .net) because the MSIL code of subs is much shorter when no value is returned. so overall subs are faster when no value is returned. oh i've just found a great source for it (talks about .net), maybe you would like to read further about it- Functions vs. Subroutines

    0 讨论(0)
  • 2021-02-07 08:18

    FWIW (my theory ;) -

    Lets think of the real world to understand this.

    Lets say you want to get something done. There are (at least) 2 ways of doing this.

    First way, send out requests for information to helpers and they will return with the information for you. so you remain in control ie all info is flowing back to you and you are deciding what to do next if any. this is more of the centrally controlled environment. this is the essence of 'function' in vba

    Second way, divide up work into separate tasks and assign responsibility to your helpers to finish the task for you ie actual work is performed here by helpers in contrast to just gathering info. This is the essence of 'sub' in vba.

    So think of what to do if code breaks. with function calls, you concentrate on the central command to look for reason of failure. With sub calls, you have to run into each sub's work and find out what they did wrong.

    Of course, you can screw up the purpose and have functions do work and subs just get info but that would just be really confusing when things break! Oh but you cant do that, read this link - http://www.cpearson.com/excel/differen.htm, which states that Excel forbids functions changing cell values and subs being called from cells.

    0 讨论(0)
  • 2021-02-07 08:18

    I found one other difference, at least on Excel but likely other Office apps. If you want to customize the ribbon by adding a button to launch a VB program, when you choose Macros in the "Choose commands from" dropdown menu, it lists any Subs in your code but not Functions. Note that a Private Sub will also be hidden from the customize ribbon selection, as will a Public Function.

    So to summarize, these will be available to add as buttons on the ribbon: Sub, Public Sub

    And these will not be available to add: Function, Public Function, Private Function, Private Sub

    0 讨论(0)
  • 2021-02-07 08:24

    I'm not absolutely sure, however, I think subs are faster than functions because the variables of a subroutine are defined upon creation of the subroutine and are accessed by referencing the memory location. Functions must allocate memory space every time they are accessed.

    Subroutines modify the variables in the calling code and functions leave them intact. So a subroutine can provide several modified pieces of information to the calling code (as many changes as there are variables, including arrays) but a function can only provide one answer at a time for the values that are passed to it. Because of this difference, if it is important that a variable in a subroutine does not change its value, one must assign the value to a temporary variable defined within the subroutine itself.

    0 讨论(0)
  • 2021-02-07 08:25

    Yes, a Function is just a Sub that returns a value.

    0 讨论(0)
  • 2021-02-07 08:30

    You will note that events are always subs, never functions. However, in MS Access, it can be useful to create functions when you wish to use them as the property of an event:

    On Close: = MyCloseFunction()
    

    Subs can return a value ByRef.

    0 讨论(0)
提交回复
热议问题