Categories
language-agnostic oop terminology

What’s the difference between a method and a function?

2071

Can someone provide a simple explanation of methods vs. functions in OOP context?

3

  • 32

    See also subroutine vs. function, function vs. procedure, coroutines.

    – gerrit

    Jan 29, 2013 at 22:44

  • It would be interesting to see another explanation outlining difference between methods and functions in r. It’s interesting as method would usually contain a function. If method is an object-dependent function then function checking for object class if(class(object)) { ... } else {stop()} would be conceptually equivalent to method?

    – Konrad

    Sep 17, 2017 at 19:18


  • 1

    Proper terminology can really depend on the language, so it can mean slightly different things in different contexts. Even so, since programmers who work with one language over another are more used to certain terminologies. Programmers are also influenced by their coworkers’ use of the terms (whether correct or incorrect). These terms are often used interchangeably. See some of the comments below, for example.

    – jvriesem

    May 18, 2020 at 3:16

2142

A function is a piece of code that is called by name. It can be passed data to operate on (i.e. the parameters) and can optionally return data (the return value). All data that is passed to a function is explicitly passed.

A method is a piece of code that is called by a name that is associated with an object. In most respects it is identical to a function except for two key differences:

  1. A method is implicitly passed the object on which it was called.
  2. A method is able to operate on data that is contained within the class (remembering that an object is an instance of a class – the class is the definition, the object is an instance of that data).

(this is a simplified explanation, ignoring issues of scope etc.)

15

  • 288

    For 1), you should also add that methods in C++ are called member functions. Thus, the difference between functions and methods in this context is analogous to the difference between functions and member functions in C++. Furthermore, languages like Java only have methods. In this case, functions would be analogous to static methods and methods would have the same meaning. For 2), you should add that a method is able to operate on the private instance (member) data declared as part of the class. Any code can access public instance data.

    Jun 13, 2012 at 21:59


  • 65

    a function is a mathematical construct. I would say all methods are functions but not all functions are methods

    May 9, 2013 at 20:22

  • 37

    Coming from a functional programming background, I feel there is a profound distinction between function and method. Mainly methods have side effects, and that functions should be pure thus giving a rather nice property of referential transparency

    – HHC

    Jul 2, 2013 at 2:26

  • 35

    @TylerGillies and HHC, I agree it might be nice if “function” and “method” meant what you wanted them to, but your definitions do not reflect some very common uses of those terms.

    Oct 15, 2013 at 5:07

  • 17

    By this definition wouldn’t a static method not actually be considered a method because it doesn’t have anything to do with a particular instance?

    May 22, 2015 at 16:44

1091

A method is on an object or is static in class.
A function is independent of any object (and outside of any class).

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

For C++ and Python it would depend on whether or not you’re in a class.
But in basic English:

  • Function: Standalone feature or functionality.
  • Method: One way of doing something, which has different approaches or methods, but related to the same aspect (aka class).

13

  • 42

    How about static methods in a class? Those would be independent of an object (in java). Wouldn’t that be a function then?

    – Squeazer

    Apr 15, 2014 at 8:00

  • 3

    In Scala: function A function can be invoked with a list of arguments to produce a result. A function has a parameter list, a body, and a result type. Functions that are members of a class, trait, or singleton object are called methods. Functions defined inside other functions are called local functions. Functions with the result type of Unit are called procedures. Anonymous functions in source code are called function literals. At run time, function literals are instantiated into objects called function values. Programming in Scala Second Edition. Martin Odersky – Lex Spoon – Bill Venners

    – Bob

    Feb 5, 2015 at 16:45


  • 14

    @Squeazer There is a question about that recently stackoverflow.com/questions/30402169/… The Java Language Specification just differs them as “class method”(static) and “instance method”. So they’re still all methods.

    – xji

    May 27, 2015 at 8:31


  • 18

    Java has lambda expressions, which are functions that are not methods

    Jan 7, 2016 at 4:07

  • 3

    In Python there are methods and functions.

    Aug 27, 2016 at 6:54

314

‘method’ is the object-oriented word for ‘function’. That’s pretty much all there is to it (ie., no real difference).

Unfortunately, I think a lot of the answers here are perpetuating or advancing the idea that there’s some complex, meaningful difference.

Really – there isn’t all that much to it, just different words for the same thing.

[late addition]


In fact, as Brian Neal pointed out in a comment to this question, the C++ standard never uses the term ‘method’ when refering to member functions. Some people may take that as an indication that C++ isn’t really an object-oriented language; however, I prefer to take it as an indication that a pretty smart group of people didn’t think there was a particularly strong reason to use a different term.

10

  • 17

    A method is a special type of function with an implicit argument passed (an instance of the class that the method is defined on). This is important as a function, in strict terms, should not use or modify anything not in its argument list.

    – ty1824

    Nov 4, 2014 at 9:39


  • 7

    @ty1824 it’s not always the case that methods are passed implicit arguments. In Python, the self argument is explicit. And many languages have static methods, which are not passed an instance.

    Jan 7, 2016 at 4:09

  • 1

    @mheiber those are some valid points. With regards to self, you are correct, it is explicitly defined. The key is that the call is implicit, based on the original object reference. There are languages that support overriding a this or self, but those constructs are then usually referred to as functions, rather than methods.

    – ty1824

    Jan 7, 2016 at 4:17

  • 1

    @mheiber As far as static methods are concerned – java implemented them as a workaround so functions could be implemented without requiring a context. I would go so far as to propose that ‘static method’ is a misleading term and should be replaced… in an ideal theoretical world 🙂

    – ty1824

    Jan 7, 2016 at 4:20

  • 2

    @ty1824 C++, Scala, Python, Ruby, JavaScript, and Scala all have static methods, so I don’t think it’s just a Java thing. I don’t like them either: they amount to using objects (bundles of state) as if they were namespaces.

    Jan 7, 2016 at 4:49