51

Possible Duplicate:
What is the difference between a method and a function

I'm trying to get my terminology correct.

What is the difference between a method and a function, in regards to C++ specifically.

Is it that a method returns nothing and just preforms operations on its class; while a function has a return value?

Jonas Stein
  • 6,826
  • 7
  • 40
  • 72
Wes
  • 4,781
  • 7
  • 44
  • 53
  • 11
    @Ash Burlaczenko I don't think it is a duplicate of that question, as that question isn't specific to C++. – Brian Neal Dec 21 '11 at 21:27
  • 8
    I agree, this is not a duplicate. In fact I even think this is a better question, since the scope is more well defined. – joaerl Oct 19 '14 at 11:36

5 Answers5

79

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).

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • 10
    And the correct terminology is not "method", it is member function. – Brian Neal Dec 21 '11 at 21:29
  • 22
    @CaptainGiraffe: I understand your point. But there is no "correct terminology" other than what's in the standard. Anything else is just ad-hoc. – Oliver Charlesworth Dec 21 '11 at 21:32
  • 1
    I'd like to add one interesting fact: Looking at libclang you'll find that they use the enum value "CXCursor_CXXMethod", which is described as "A C++ class method." ( http://clang.llvm.org/doxygen/group__CINDEX.html ) – Daniel Jour May 10 '15 at 15:29
  • @BrianNeal Re: "member function": perhaps more precisely "class member function". – pmor Jan 13 '23 at 08:45
  • @DanielJour FYI: In LLVM's source code I see that "method" is frequently used. For people who study / studied C++ by reading C++ standard using of term "method" may be confusing. – pmor Jan 13 '23 at 08:51
28

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.

Brian Neal
  • 31,821
  • 7
  • 55
  • 59
  • 7
    And for the proper terminology we look to the standard. Heck, even look in Stroustrup's books. He always calls them member functions, not methods. – Brian Neal Dec 21 '11 at 21:36
  • Thanks! As of 9 June 2018, in the GitHub draft I find [12 code results for method](https://github.com/cplusplus/draft/search?utf8=%E2%9C%93&q=method&type=) and [75 code results for function](https://github.com/cplusplus/draft/search?q=function&unscoped_q=function). – pzrq Jun 09 '18 at 08:45
  • @pzrq which are either totally unrelated or involve specifically change occurrences of *method* in this sense to *member function*, thus supporting the conclusion that C++ does not *intentionally, permanently* use the term *method* to mean a function operating on a class. – underscore_d Nov 17 '18 at 20:29
7

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
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • 1
    nice example. Although the last one confused me. The one which goes "A ainst;" what is this? – Pranav Nov 22 '19 at 14:54
  • It too me some time to understand, "A" is the class name and "ainst" means object. (Lockdown improved my programming sense a little bit.) – Pranav Jul 27 '20 at 07:53
4

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
T.N.
  • 87
  • 10
Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67
0

Even a method can have a return value.

A method is a function of a class. For example class "car" has a method "accelerate".

Christian Nowak
  • 936
  • 5
  • 5