6

Why are they termed differently?

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

2
  • 2
    Serial upvoter, coming through! Commented Sep 6, 2012 at 14:56
  • @Forty-Two - I second that, trolling's annoying when you're trying to get things done, distracting from the discussion. Commented Sep 6, 2012 at 15:01

6 Answers 6

12

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

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

Why the -1 this is my understanding too ?
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.

4 Comments

serial downvoting, awesome :P
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
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.
@jbabey the serial downvoting has been reversed.
3

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

Comments

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?

1 Comment

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.
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.

16 Comments

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).
@IanHenry: And it should be noted that there are languages that call (some of) their methods "function", such as Delphi.
@O.R.Mapper This is true in JavaScript as well, hence, I think, the confusion that led to this question.
@jbabey: So from a language-agnostic point of view, a static method is not a method?
@jbabey: Unless you bring polymorphism in subclasses into the mix.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.