Difference between Methods and Functions


Major Difference

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.

  1. It is implicitly passed the object for which it was called
  2. 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)

(this is a simplified explanation, ignoring issues of scope etc.)

Functions

  1. Functions have independent existence means they can be defined outside of the class. Ex:- main() function in C, C++ Language
  2. Functions are defined in structured languages like Pascal,C and object based language like javaScript
  3. Functions are called independently.
  4. Functions are self describing unit of code.
  5. I was taught that “A Function should do 1(one) thing and do

Methods

  1. Methods do not have independent existence they are always defined with in class. Method is tied to a specific class. Ex:- main() method in Java that is defined with in a class
  2. Methods are defined in object oriented languages like C#, Java
  3. Methods are called using instance or object.
  4. Methods are used to manipuate instance variable of a class.

 

Sources:

1.  http://stackoverflow.com/questions/155609/what-is-the-difference-between-a-method-and-a-function

2. http://www.dotnet-tricks.com/Tutorial/oops/4a0J170612-Difference-between-function-and-method.html

About pacesettergraam

A good and realistic person

Posted on December 22, 2013, in Uncategorized and tagged , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: