method vs function vs procedure vs class?

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.


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


Function, method and procedure are homogeneous and each of them is a subroutine that performs some calculations.

A subroutine is:

  • a method when used in Object-Oriented Programming (OOP). A method can return nothing (void) or something and/or it can change data outside of the subroutine or method.
  • a procedure when it does not return anything but it can change data outside of the subroutine, think of a SQL stored procedure. Not considering output parameters!
  • a function when it returns something (its calculated result) without changing data outside of the subroutine or function. This is the way how SQL functions work.

After all, they are all a piece of re-usable code that does something, e.g. return data, calculate or manipulate data.

Tags:

C

Oop

Objective C