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)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Oldest comments (3)
Function is only that, method is a function inside a class.
A method is a function in a object oriented context.
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)