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

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

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

相关标签:
30条回答
  • 2020-11-21 05:34

    TL;DR

    A Function is a piece of code to run.
    A Method is a Function inside an Object.

    Example of a function:

    
    function sum(){
      console.log("sum")l
    }
    

    Example of a Method:

    const obj = {
    a:1,
    b:2,
    sum(){
      }
    }
    

    So thats why we say that a "this" keyword inside a Function is not very useful unless we use it with call, apply or bind .. because call, apply, bind will call that function as a method inside object ==> basically it converts function to method

    0 讨论(0)
  • 2020-11-21 05:35

    If you feel like reading here is "My introduction to OO methods"

    The idea behind Object Oriented paradigm is to "threat" the software is composed of .. well "objects". Objects in real world have properties, for instance if you have an Employee, the employee has a name, an employee id, a position, he belongs to a department etc. etc.

    The object also know how to deal with its attributes and perform some operations on them. Let say if we want to know what an employee is doing right now we would ask him.

    employe whatAreYouDoing.
    

    That "whatAreYouDoing" is a "message" sent to the object. The object knows how to answer to that questions, it is said it has a "method" to resolve the question.

    So, the way objects have to expose its behavior are called methods. Methods thus are the artifact object have to "do" something.

    Other possible methods are

    employee whatIsYourName
    employee whatIsYourDepartmentsName
    

    etc.

    Functions in the other hand are ways a programming language has to compute some data, for instance you might have the function addValues( 8 , 8 ) that returns 16

    // pseudo-code
    function addValues( int x, int y )  return x + y 
    // call it 
    result = addValues( 8,8 )
    print result // output is 16...
    

    Since first popular programming languages ( such as fortran, c, pascal ) didn't cover the OO paradigm, they only call to these artifacts "functions".

    for instance the previous function in C would be:

    int addValues( int x, int y ) 
    {
       return x + y;
    }
    

    It is not "natural" to say an object has a "function" to perform some action, because functions are more related to mathematical stuff while an Employee has little mathematic on it, but you can have methods that do exactly the same as functions, for instance in Java this would be the equivalent addValues function.

    public static int addValues( int x, int y ) {
        return x + y;
    }
    

    Looks familiar? That´s because Java have its roots on C++ and C++ on C.

    At the end is just a concept, in implementation they might look the same, but in the OO documentation these are called method.

    Here´s an example of the previously Employee object in Java.

    public class Employee {
    
        Department department;
        String name;
    
        public String whatsYourName(){
            return this.name;
        }
        public String whatsYourDeparmentsName(){
             return this.department.name();
        }
        public String whatAreYouDoing(){
            return "nothing";
        } 
        // Ignore the following, only set here for completness
        public Employee( String name ) {
            this.name = name;
        }
    
    }
    
    // Usage sample.
    Employee employee = new Employee( "John" ); // Creates an employee called John
    
    // If I want to display what is this employee doing I could use its methods.
    // to know it.
    String name = employee.whatIsYourName():
    String doingWhat = employee.whatAreYouDoint();
    
    // Print the info to the console.
    
     System.out.printf("Employee %s is doing: %s", name, doingWhat );
    
    Output:
    Employee John is doing nothing.
    

    The difference then, is on the "domain" where it is applied.

    AppleScript have the idea of "natural language" matphor , that at some point OO had. For instance Smalltalk. I hope it may be reasonable easier for you to understand methods in objects after reading this.

    NOTE: The code is not to be compiled, just to serve as an example. Feel free to modify the post and add Python example.

    0 讨论(0)
  • 2020-11-21 05:35

    I am not an expert, but this is what I know:

    1. Function is C language term, it refers to a piece of code and the function name will be the identifier to use this function.

    2. Method is the OO term, typically it has a this pointer in the function parameter. You can not invoke this piece of code like C, you need to use object to invoke it.

    3. The invoke methods are also different. Here invoke meaning to find the address of this piece of code. C/C++, the linking time will use the function symbol to locate.

    4. Objecive-C is different. Invoke meaning a C function to use data structure to find the address. It means everything is known at run time.

    0 讨论(0)
  • 2020-11-21 05:37

    Function is a set of logic that can be used to manipulate data.

    While, Method is function that is used to manipulate the data of the object where it belongs. So technically, if you have a function that is not completely related to your class but was declared in the class, its not a method; It's called a bad design.

    0 讨论(0)
  • 2020-11-21 05:38

    A method is on an object.
    A function is independent of an object.

    For Java and C#, there are only methods.
    For C, there are only functions.

    For C++ and Python it would depend on whether or not you're in a class.

    0 讨论(0)
  • 2020-11-21 05:38

    Simple way to remember:

    • Function → Free (Free means not belong to an object or class)
    • Method → Member (A member of an object or class)
    0 讨论(0)
提交回复
热议问题