In C++, what is the difference between a method and a function

The term "Method" is not used in c++, but rather member function.

If you are thinking about the difference between a procedure and a function then the difference in c++ is none. Pascal was pretty much the last language to make that distinction. (ADA was constructed later and used the term Procedure, thanks Brian Neal.)

Any function, member or not, declared as void, would be a Procedure in the old vocabulary.

A member function is a complex beast, a function is a simple function.

A member function

  • is a member of a class
  • can be private
  • can be protected
  • can be public
  • can be virtual
  • can be pure virtual

As far as the C++ standard is concerned, there is no such thing as a "method". This terminology is used in other OO languages (e.g. Java) to refer to member functions of a class.

In common usage, you'll find that most people will use "method" and "function" more or less interchangeably, although some people will restrict use of "method" to member functions (as opposed to "free functions" which aren't members of a class).


A method is a member function of a class, but in C++ they are more commonly called member functions than methods (some programmers coming from other languages like Java call them methods).

A function is usually meant to mean a free-function, which is not the member of a class.

So while a member function is a function, a function is not necessarily a member function.

Example:

void blah() { } // function

class A {
    void blah() { } // member function (what would be a "method" in other languages)
};

blah(); // free functions (non-member functions) can be called like this

A ainst;
ainst.blah(); // member functions require an instance to invoke them on

Sorry, but this is one of my pet peeves. Method is just a generic OO-type term. Methods do not exist in C++. If you open the C++ standard, you won't find any mention of "methods". C++ has functions, of various flavors.