Purpose of using sub routines over functions

后端 未结 7 1132
抹茶落季
抹茶落季 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: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.

提交回复
热议问题