[Tutor] methods vs. functions

Robert Sjoblom robert.sjoblom at gmail.com
Tue Aug 23 01:10:07 CEST 2011


> Can someone please explain the difference between methods and functions?
I've heard the explanation "A cow is an animal but an animal is not
necessarily a cow." used to describe the difference between methods
and functions (ie, all methods are functions, but not all functions
are methods). Obviously, that's a horrible description.

This one's better:
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)

Credit where credit is due:
http://stackoverflow.com/questions/155609/what-is-the-difference-between-a-method-and-a-function

/Robert S.


More information about the Tutor mailing list