6

Why are they termed differently?

Isn't the nature/purpose/place of a function identical to that of a method?

6 Answers6

12

Functions stand alone and methods are members of a class. That's all. (it's really all the same.)

Kaushal
  • 471
  • 3
  • 15
10

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
9

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.

Community
  • 1
  • 1
jbabey
  • 45,965
  • 12
  • 71
  • 94
  • 2
    serial downvoting, awesome :P – jbabey Sep 06 '12 at 14:50
  • 1
    Again this is my understanding function is standalone method is on an object heck javascript has both functions and methods since you can declare a function and you can attach it to a object where by it becomes a method call and changes the value of `this` – Mark Broadhurst Sep 06 '12 at 14:53
  • Functions belonging to a *class* but not necessarily to a particular *instance* are still commonly called static *methods*. Might be worth noting that in your answer. – danielkza Sep 12 '12 at 23:58
  • @jbabey the serial downvoting has been reversed. – Andrew Grimm Jun 22 '16 at 07:30
3

You usually find the term function when dealing with procedural code. OOP uses the term method but they are the same thing.

Stephen Gilboy
  • 5,572
  • 2
  • 30
  • 36
3

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.

  • It is implicitly passed the object for which it was called
  • It is able to operate on data that is contained within the class (remembering that an object is an instance of a class - the class is the definition, the object is an instance of that data)

Also, another answer: Difference between function and method?

Community
  • 1
  • 1
cassi.lup
  • 1,261
  • 7
  • 23
  • 1
    _All data that is passed to a function is explicitly passed._: I disagree. JavaScript is a functional language. whenever a function is called a call-object is created. Part of this object is the `arguments` object, this isn't explicitly passed. Besides: closures don't really pass arguments, but the scopes are linked to the function all the same. JavaScript and C# are like boats and cars, they both move, have engines etc... but their based on different concepts. – Elias Van Ootegem Sep 06 '12 at 14:57
2

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.

Freeman
  • 5,691
  • 3
  • 29
  • 41
  • This is not correct. There is a distinct difference between functions and methods: methods are functions "attached" to instances of objects. The fact that the term "method" is more prevalent nowadays than in older programming languages is simply because object-oriented programming has become so popular. But it's perfectly reasonable to have a language with both functions and methods (as in Python). – Ian Henry Sep 06 '12 at 14:51
  • so i uderstand you are the serial downvoter? and i said generally the same thing not exactly. – Freeman Sep 06 '12 at 14:52
  • @Freeman you were most likely downvoted because your answer is missing the key piece of information that separates functions from methods. – jbabey Sep 06 '12 at 14:52
  • No; I downvoted this one only (it was the first answer I saw and was writing my response while the serial downvoter came through). Not sure who downvoted everyone else. – Ian Henry Sep 06 '12 at 14:52
  • 1
    @IanHenry: And it should be noted that there are languages that call (some of) their *methods* "function", such as Delphi. – O. R. Mapper Sep 06 '12 at 14:54
  • @jbabey, how did you get to that conclusion? im just curious altough i assure you i hate trolling as much as the next guy. – Freeman Sep 06 '12 at 14:54
  • @Freeman the key difference is that a method is a specific type of function that exists on the instance of an object in an object-oriented program. – jbabey Sep 06 '12 at 14:55
  • 1
    @O.R.Mapper This is true in JavaScript as well, hence, I think, the confusion that led to this question. – Ian Henry Sep 06 '12 at 14:55
  • @jbabey: That would imply that static methods are not methods. – O. R. Mapper Sep 06 '12 at 14:55
  • @O.R.Mapper some languages use method/function/procedure/etc interchangably - i am speaking from a language-agnostic point of view. – jbabey Sep 06 '12 at 14:56
  • 1
    @jbabey: So from a language-agnostic point of view, a static method is not a method? – O. R. Mapper Sep 06 '12 at 14:58
  • @O.R.Mapper "static method" and "function" are synonymous; both mean a segment of code that is not bound to the context of any specific object. – jbabey Sep 06 '12 at 15:00
  • @jbabey yet Microsoft still calls it 'static method' . – Freeman Sep 06 '12 at 15:01
  • 1
    @jbabey: Unless you bring polymorphism in subclasses into the mix. – O. R. Mapper Sep 06 '12 at 15:02
  • @O.R.Mapper Well, a static method still belongs to a class, just not an instance of that class. This is contrary to, say C++, where there's a difference between a static method in a class and a function that is not inside the definition of any class at all. – Servy Sep 06 '12 at 15:02
  • @Servy: I totally agree with that statement, I was just trying to find out what definition jbabey was actually trying to cling to. – O. R. Mapper Sep 07 '12 at 06:53