DEV Community

Tristán
Tristán

Posted on

What's difference between Function and Method?

Top comments (3)

Collapse
 
creativcoder profile image
creativcoder •

Both of them refer to any chunk of code that can be reused again with different arguments and may or may not cause side effects. Traditionally called a "subroutine". They are named differently because of the different context they appear within.

A method is a subroutine that is part of the instantiation of a class (an object) in an object oriented programming language, whereas a function is a subroutine not tied to any class/object. They are also referred to as free functions sometimes.
Another thing to know from the functional programming aspect is that functions have a property that they cannot cause side effects.

Method can only be called if you have an instance of some class, whereas to call a function all you need is to provide it the required arguments it needs.

Another interpretation: a method is just a function that receives the object instance as its first argument. (Ex: the self keyword in python)

Collapse
 
atlasoss profile image
Florian •

A method is a function in a object oriented context.