开发者

Difference between functions and method in a simple way?

It may be sound s开发者_Python百科illy. But trust me, there are lots of programmers around with out knowing the difference. i don't find any clear and simple way of explanation for this question in googling. Please don't say this is duplicate. give me a simple example and explanation. Hope after your answer I would able to tell the difference between these two even in my middle of the sleep.


I doubt this is an official definition.
But this is the way I think about it.

A function is freestanding (and should have no side effects)

A method has an associated object that it interacts with (and can store state in)

I also use the term procedure: for a function that is naughty and modifies some global state and thus has side affects. But my code never contains procedures (so I don't think about it much).


A member function of class is called it's method. A method is a function, but a function is not nececcarily a method.

void justFunction()
{
   std::cout << "Just a function\n";
}

class MyClass
{
   public:
   void memberFunction()
   {
      std::cout << "Member function\n";
   }
};

int main ()
{
   justFunction(); // calling global function
   MyClass a;
   a.memberFunction(); // calling a member function
};


Function is a procedure that returns some value. Method is a procedure which is member of the class.

By procedure here I refer to block of code which can be invoked with parameters, not as "a function that returns nothing".


did you check the tag descriptions?

  • a function takes some input and returns a result (like a math function f(x))

  • a method does some actions depending on the input

most of the time these are interchangeable


The question is interesting and the number of non-coherent answers shows that there are multiple understandings of method/function. The definition can also differ between different languages. Here is my understanding, in C++, plain and simple:

Functions stand alone and methods are member functions of a class. Thus, all methods are functions but not all functions are methods.

I.e:

class MyClass {
public:
    int MyMethod() { return 5; } // method
};


int MyFunction() { return 7; } // function


A method is on an object. A function is independent of an object.

For Java, there are only methods. For C, there are only functions.

For C++ it would depend on whether or not you're in a class.

Source


In short.

A method is in the scope of a class or object.

A function is not.


Longer versions.

Think about objects as pieces of a greater system. So your application has a scope that is global and inside that scope you have objects that describe... well... objects. A good real world example I like to use is: if you have a program say your house and inside your home you have many objects, table, sink, and person. Now a person can turn on a sink. So the person has the method turn on, and the sink would have the property On. It may look something like this

person.turn_on(sink)

Now a sink cannot turn on a person... (well maybe that thats another story) Anyway, this would fail

sink.turn_on(person)

If these were not methods and were "functions instead you may have something that looked like this

function turn_on (who, what) #some app code to turn it on end

Then you could call it like this

turn_on(person, sink)

This doesn't make sense for a program, why would a table be able to be called to turn something on. Why not error sooner?

Which is more descriptive?

Now think about it from the tables perspective a table cannot be turned on or turn anything on so it would not have those two attributes (method and local variable)

I hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜