What is a difference between a method and a function?

后端 未结 7 1454
礼貌的吻别
礼貌的吻别 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:43

    Method is actually a function used in the context of a class/object.

    When you create a function outside of a class/object, you can call it a function but when you create a function inside a class, you can call it a method.

    class foo {
       public function bar() { // a method
         ........
       }
    }
    

    function bar() {  // a function not part of an object
    }
    

    So an object can have methods (functions) and properties (variables).

提交回复
热议问题