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

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

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

30条回答
  •  北荒
    北荒 (楼主)
    2020-11-21 05:53

    In OO world, the two are commonly used to mean the same thing.

    From a pure Math and CS perspective, a function will always return the same result when called with the same arguments ( f(x,y) = (x + y) ). A method on the other hand, is typically associated with an instance of a class. Again though, most modern OO languages no longer use the term "function" for the most part. Many static methods can be quite like functions, as they typically have no state (not always true).

提交回复
热议问题