What is a difference between a method and a function?

后端 未结 7 1477
礼貌的吻别
礼貌的吻别 2020-12-08 00:15

What is the difference between a method and a function? Is it that a method returns a value and a function doesn\'t?

7条回答
  •  感动是毒
    2020-12-08 00:50

    The words are not opposed to each other but rather describes two possible aspects of a subroutine. An attempt to define the words follows:

    Subroutine: A set of instructions that can be used several times in the same program.

    Function: A subroutine that returns a value. Derived from functions in mathematics (wikipedia).

    Method: A subroutine that belongs to an object or a class. Could be a function.

    I tend to use the word "function" for every subroutine that has no side effects but returns one clear value and the word "method" for every subroutine that has a side effect.

提交回复
热议问题