Difference between functions and method in a simple way?

That sounds stupid. But believe me, there are many programmers who do not know the difference. I did not find a clear and simple way to explain this issue in search engines. Please do not say that this is a duplicate. give me a simple example and explanation. Hope after your answer I could tell the difference between the two even in the middle of a dream.

+4
source share
7 answers

A member function of a class is called its 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 }; 
+1
source

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

A function is autonomous (and should not have side effects)

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

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

+1
source

The question is interesting, and the number of incoherent answers shows that there are several concepts of a method / function. The definition may also vary between different languages. Here is my understanding, in C ++, simple and simple:

Functions are autonomous, and methods are member functions 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 
0
source

A function is a procedure that returns some value. A method is a procedure that is a member of a class.

In this procedure, I refer to a block of code that can be called with parameters, and not as a "function that returns nothing."

0
source

Have you checked the tag descriptions?

  • the function takes some input and returns the result (for example, the mathematical function f(x) )

  • the method performs some actions depending on the input

in most cases they are interchangeable

0
source

The method is on the object. The function is independent of the object.

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

For C ++, this will depend on whether you are in the class.

A source

0
source

In short.

The method is in the scope of the class or object.

There is no function.


Longer versions.

Think of objects as pieces of a larger system. So your application has a global scope, inside of which there are objects that describe ... well ... objects. A good example of the real world that I like to use: if you have a program that talks about your home and in your home, you have a lot of objects, a table, a sink and a person. Now a person can turn on the sink. Thus, the person has a way of inclusion, and the shell will have the On property. It might look something like this.

 person.turn_on(sink) 

Now the sink can’t turn on a person ... (well, maybe this is another story) Anyway, it won’t work out

sink.turn_on (person)

If they were not methods and were “functions”, you could have something like this

function turn_on (who what) # some app code to enable its end

Then you can call it as follows

 turn_on(person, sink) 

This does not make sense for the program, why one could invoke a table to include something. Why wasn't it a mistake before?

What is more descriptive?

Now think about it in terms of tables, the table cannot be included or change anything, so it will not have these two attributes (method and local variable)

I hope this helps

-1
source

All Articles