What's the difference between a method and a function?

前端 未结 30 3246
粉色の甜心
粉色の甜心 2020-11-21 05:08

Can someone provide a simple explanation of methods vs. functions in OOP context?

30条回答
  •  无人及你
    2020-11-21 05:46

    Historically, there may have been a subtle difference with a "method" being something which does not return a value, and a "function" one which does.Each language has its own lexicon of terms with special meaning.

    In "C", the word "function" means a program routine.

    In Java, the term "function" does not have any special meaning. Whereas "method" means one of the routines that forms the implementation of a class.

    In C# that would translate as:

    public void DoSomething() {} // method
    public int DoSomethingAndReturnMeANumber(){} // function
    

    But really, I re-iterate that there is really no difference in the 2 concepts. If you use the term "function" in informal discussions about Java, people will assume you meant "method" and carry on. Don't use it in proper documents or presentations about Java, or you will look silly.

提交回复
热议问题