Methods Vs Functions in OOP context

Methods Vs Functions in OOP context

I used to think that methods and functions are the same. Once in an interview I was asked a question relating to OOP programming concepts. In my explanation I used both methods and functions to mean the same thing. The next question I was asked was :

What's the difference between the methods and functions?

I was quick to give an answer that perpetuated the idea that they are the same. This was fuzzy. As much as you would think that they are the same, within oop context they are not. Both functions and methods are pieces of codes that are written to carry a specific task. The difference between the two is that a method is associated with an object while a function is independent of an object.

A function is called by name and data can be passed to it as parameters to operate on. It can optionally have a return value and also all data is passed to it explicitly.

A key distinction of a method from a function is that it can operate on data contained in a class, keeping in mind that an object is an instance of a class. A method is also implicitly passed the object on which it is called.

A general and simple definition of the difference is to think of functions as being defined outside a class and methods as being defined inside a class and/or are part of the class.

In general programming paradigm, functions are considered as part of the Procedural Programming Paradigm where as methods are considered part of the Object Oriented Paradigm.

It is important to keep in mind that while all methods are functions, not all functions are methods.

Cover Photo by Christopher Kuszajewski from Pixabay