tags:

views:

254

answers:

2

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 ?

+1  A: 

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

Starx
Traditionally, the difference between a procedure and a function is that a function returns a value while a procedure doesn't. C-style languages do not make this distinction -- everything is a function, it just might return `void`.
walkytalky
@walkytalky, thanks for adding that
Starx
+9  A: 

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.

JUST MY correct OPINION
The function/procedure difference isn't really being anal - it's a really good idea, and is often called command/query separation.
kyoryu
All that means is that you're being as anal retentive (or is it "anal-retentive"?) as I am. ;)
JUST MY correct OPINION
Thanks can u provide with me example. for differentiating between this ?
hey I'm confused in differentiating of Methods vs Function ??
A method **is** a function/procedure. It's just associated with a class in some way. Look above at the sample pseudo-C++ I gave you: `getMemberVariableFunction` and `setMemberVariableProcedure` are both **inside** the class `myClass`. They are *part of the class*. `plainOldProcedure` and `plainOldFunction` are defined wholly outside of the class. They are not part of the class and don't have any special status (like the `this` pointer in C++) available to them. Nor do they have any status in the protection mechanisms of the class.
JUST MY correct OPINION
Thank you bro .. You Opinion is fully correct .. now i got it..