You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What is the difference between a Function and a Method ?
A function is a piece of code that is called by name. It can be passed data to operate on (i.e. 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 a name that is associated with an object.
In most respects it is identical to a function except for two key differences:
1. A method is implicitly passed the object on which it was called.
2. A method 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).
They're often interchangeable, but a method usually is a function of a class, and a function is usually outside the class.
In Java, where everything (except package and import statements) must be inside the class, people might always refer to them as "methods".