Difference between function and method

This question already has an answer here:

  • Difference between a method and a function 33 answers

  • Function or a method is a named callable piece of code which performs some operations and optionally returns a value.

    In c language the term function is used. Java & C# people call these methods (and a function in this case is defined within a class/object).

    A C++ programmer might call it a function or sometimes method (depending on if they are writing procedural style c++ code or are doing object oriented way of c++).

    You call a function by just calling it's name like result = mySum(num1, num2); You would call a method by referencing its object first like

    result = MyCalc.mySum(num1,num2);
    

    Check the link in CubanAzucy's answer. It is already discussed in great detail on Stack Overflow.


    A function is a piece of code that is called by name. It can be passed data to operate on (ie. the parameters) and can optionally return data (the return value).

    All data that is passed to a function is explicitly passed.

    A method is a piece of code that is called by name that is associated with an object. In most respects it is identical to a function except for two key differences.

    It is implicitly passed the object for which it was called It is able to operate on data that is contained within the class (remembering that an object is an instance of a class - the class is the definition, the object is an instance of that data)

    In general: methods are functions that belong to a class, functions can be on any other scope of the code so you could state that all methods are functions, but not all functions are methods:

    Take the following python example:

    class Door:
      def open(self):
        print 'hello stranger'
    
    def knock_door:
      a_door = Door()
      Door.open(a_door)
    
    knock_door()
    

    The example given shows you a class called "Door" which has a method or action called "open", it is called a method because it was declared inside a class. There is another portion of code with "def" just below which defines a function, it is a function because it is not declared inside a class, this function calls the method we defined inside our class as you can see and finally the function is being called "alone".

    As you can see you can call a function anywhere but if you want to call a method either you have to pass a new object of the same type as the class the method is declared (Class.method(object)) or you have to invoke the method inside the object (object.Method()), at least in python.

    Think of methods as things only one entity can do, so if you have a Dog class it would make sense to have a bark function only inside that class and that would be a method, if you have also a Person class it could make sense to write a function "feed" for that doesn't belong to any class since both humans and dogs can be feed and you could call that a function since it does not belong to any class in particular.

    链接地址: http://www.djcxy.com/p/17316.html

    上一篇: 如何区分方法和函数?

    下一篇: 功能和方法之间的区别