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

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

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

30条回答
  •  不思量自难忘°
    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.

提交回复
热议问题