Why are they termed differently?
Isn't the nature/purpose/place of a function identical to that of a method?
Why are they termed differently?
Isn't the nature/purpose/place of a function identical to that of a method?
Functions stand alone and methods are members of a class. That's all. (it's really all the same.)
A method is a function that is attached to an object.
JavaScript functions are usually referred to as methods when used in the context of an object.
e.g. document.getElementById('foo')
uses the getElementById
method.
Function is a very old term meaning a segment of code that accomplishes some task (does some function, if you will).
Method is a relatively newer term (came with OO programming) that means a function that belongs to a specific instance of an object.
All methods are functions, but not all functions are methods.
See this related question from the programmers stackexchange.
You usually find the term function when dealing with procedural code. OOP uses the term method but they are the same thing.
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.
Also, another answer: Difference between function and method?
Well, its all about the names, but generally functions and methods are the same thing, and of course have the same purpose.
It all began with the first programming languages, where they where called functions, but as more higher level programming languages arose, i guess they thought to name them methods even if they are and serve the sasme purpose.
EDIT:
Functions are not part of any object or class. Methods are a memeber of an object or class.