June 14, 2010

method vs function vs procedure vs class?

Question by 2easylogic

I know the basics of this methods,procedures,function and classes but i always confuse to differentiate among those in contrast of Object oriented programming so please can any body tell me the difference among those with simple examples ?

Answer by JUST MY correct OPINION

A class, in current, conventional OOP, is a collection of data (member variables) bound together with the functions/procedures that work on that data (member functions or methods). The class has no relationship to the other three terms aside from the fact that it “contains” (more properly “is associated with”) the latter.

The other three terms … well, it depends.

A function is a collection of computing statements. So is a procedure. In some very anal retentive languages, though, a function returns a value and a procedure doesn’t. In such languages procedures are generally used for their side effects (like I/O) while functions are used for calculations and tend to avoid side effects. (This is the usage I tend to favour. Yes, I am that anal retentive.)

Most languages are not that anal retentive, however, and as a result people will use the terms “function” and “procedure” interchangeably, preferring one to the other based on their background. (Modula-* programmers will tend to use “procedure” while C/C++/Java/whatever will tend to use “function”, for example.)

A method is just jargon for a function (or procedure) bound to a class. Indeed not all OOP languages use the term “method”. In a typical (but not universal!) implementation, methods have an implied first parameter (called things like this or self or the like) for accessing the containing class. This is not, as I said, universal. Some languages make that first parameter explicit (and thus allow to be named anything you’d like) while in still others there’s no magic first parameter at all.


Edited to add this example:

The following untested and uncompiled C++-like code should show you what kind of things are involved.

class MyClass
{
  int memberVariable;

  void setMemberVariableProcedure(int v)
  {
    memberVariable = v;
  }

  int getMemberVariableFunction()
  {
    return memberVariable;
  }
};

void plainOldProcedure(int stuff)
{
  cout << stuff;
}

int plainOldFunction(int stuff)
{
  return 2 * stuff;
}

In this code getMemberVariableProcedure and getMemberVariableFunction are both methods.

Answer by Starx

Procedures, function and methods are generally alike, they hold some processing statements.

The only differences I can think between these three and the places where they are used.

I mean ‘method’ are generally used to define functions inside a class, where several types of user access right like public, protected, private can be defined.

“Procedures”, are also function but they generally represent a series of function which needs to be carried out, upon the completion of one function or parallely with another.


Classes are collection of related attributes and methods. Attributes define the the object of the class where as the methods are the action done by or done on the class.

Hope, this was helpful

...

Please fill the form - I will response as fast as I can!